yii實現(xiàn)級聯(lián)下拉菜單的方法
本文詳細講述了yii實現(xiàn)級聯(lián)下拉菜單的方法,具體步驟如下:
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() 這個是先讀取一個下拉菜單。調(diào)用OrderScr model中的options方法。內(nèi)容如下
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代碼,實現(xiàn)當?shù)谝粋€下拉菜單變化時給第二個下拉菜單進行內(nèi)容賦值。
<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代碼中,實現(xiàn)調(diào)取一個程序獲取第二個下拉菜單的值(調(diào)用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; }
相關文章
thinkphp5.1框架實現(xiàn)格式化mysql時間戳為日期的方式小結
這篇文章主要介紹了thinkphp5.1框架實現(xiàn)格式化mysql時間戳為日期的方式,結合實例形式分析了thinkPHP針對mysql時間戳格式轉換的相關操作技巧,需要的朋友可以參考下2019-10-10ThinkPHP中__initialize()和類的構造函數(shù)__construct()用法分析
這篇文章主要介紹了ThinkPHP中__initialize()和類的構造函數(shù)__construct()用法,以實例形式分析了ThinkPHP中類的初始化時構造子類的方法,是采用ThinkPHP進行面向對象程序設計中比較重要的概念,需要的朋友可以參考下2014-11-11PHP + plupload.js實現(xiàn)多圖上傳并顯示進度條加刪除實例代碼
本篇文章主要介紹了PHP + plupload.js實現(xiàn)多圖上傳并顯示進度條加刪除實例代碼。具有一定的參考價值,有興趣的可以了解一下。2017-03-03php導出csv數(shù)據(jù)在瀏覽器中輸出提供下載或保存到文件的示例
這篇文章主要介紹了php導出csv數(shù)據(jù)在瀏覽器中輸出提供下載或保存到文件的示例,需要的朋友可以參考下2014-04-04