yii實現級聯下拉式功能表的方法_php執行個體

來源:互聯網
上載者:User

本文詳細講述了yii實現級聯下拉式功能表的方法,具體步驟如下:

1.模版中加入如下代碼:

<?php echo $form->dropDownList($model, 'src_type_id', OrderSrc::options(), array( <span style="white-space:pre"> </span>'id' => 'task-order-src-id', )); echo $form->dropDownList($model, 'src_shop_id', array(''=>'全部'), array( <span style="white-space:pre"> </span>'id' => 'task-shop-id', ))?>

在這段代碼中,OrderSrc_options() 這個是先讀取一個下拉式功能表。調用OrderScr model中的options方法。內容如下

public static function options($hasShop = true) { $model = new self(); if($hasShop) $model->hasShop(); $models = $model->findAll(); $array = array(''=>'全部'); foreach($models as $model) { $array[$model->src_id] = $model->src_name; } return $array;}

2.然後在模版頁面中增加JS代碼,實現當第一個下拉式功能表變化時給第二個下拉式功能表進行內容賦值。

<script type='text/javascript'>$().ready(function(e) { $('#task-order-src-id').change(function(e) { refreshShops(); }); refreshShops(); function refreshShops() { $.get('<?php echo $this->createUrl('getShops')?>', {  'srcId': $('#task-order-src-id').val() }, function(html_content) {  $('#task-shop-id')  .html(html_content)  .find('option[value=<?php echo $model->src_shop_id?>]')   .attr('selected', 'selected'); }); }});</script>

在這段JS代碼中,實現調取一個程式擷取第二個下拉式功能表的值(調用Controller中的actionGetShops方法),任何追加到第二個下拉式功能表中。

Controller中的actionGetShops方法如下:

public function actionGetShops() { $srcId = $_GET['srcId']; $array = ThirdpartInterfaceConfig::options($srcId); $htmlContent = "<option value=''>全部</options>"; foreach($array as $k=>$v) { $htmlContent .= "<option value='{$k}'>{$v}</option>"; } echo $htmlContent;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.