yii實現(xiàn)級聯(lián)下拉菜單的方法
本文詳細(xì)講述了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)當(dāng)?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;
}
相關(guān)文章
thinkphp5.1框架實現(xiàn)格式化mysql時間戳為日期的方式小結(jié)
這篇文章主要介紹了thinkphp5.1框架實現(xiàn)格式化mysql時間戳為日期的方式,結(jié)合實例形式分析了thinkPHP針對mysql時間戳格式轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
ThinkPHP中__initialize()和類的構(gòu)造函數(shù)__construct()用法分析
這篇文章主要介紹了ThinkPHP中__initialize()和類的構(gòu)造函數(shù)__construct()用法,以實例形式分析了ThinkPHP中類的初始化時構(gòu)造子類的方法,是采用ThinkPHP進行面向?qū)ο蟪绦蛟O(shè)計中比較重要的概念,需要的朋友可以參考下2014-11-11
PHP + plupload.js實現(xiàn)多圖上傳并顯示進度條加刪除實例代碼
本篇文章主要介紹了PHP + plupload.js實現(xiàn)多圖上傳并顯示進度條加刪除實例代碼。具有一定的參考價值,有興趣的可以了解一下。2017-03-03
微信開發(fā)之網(wǎng)頁授權(quán)獲取用戶信息(二)
本文給大家闡述的微信開發(fā)基于yii2.0框架,對微信開發(fā)之網(wǎng)頁授權(quán)獲取用戶信息相關(guān)知識感興趣的朋友通過本文學(xué)習(xí)吧2016-01-01
php導(dǎo)出csv數(shù)據(jù)在瀏覽器中輸出提供下載或保存到文件的示例
這篇文章主要介紹了php導(dǎo)出csv數(shù)據(jù)在瀏覽器中輸出提供下載或保存到文件的示例,需要的朋友可以參考下2014-04-04

