本文介绍了如何在yii2中创建依赖项下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在backend/views/_form.php
<?php $CoursesCat = ArrayHelper::map(CoursesCat::find()->all(),'id', 'cat_name');
$CoursesSubcat = ArrayHelper::map(CoursesSubcat::find()->all(),'id', 'name');
$form = ActiveForm::begin(); ?>
<?php
echo $form->field($model, 'cat_id')->dropDownList($CoursesCat,
['prompt'=>'-Choose a Category-',
'onchange'=>'
$.post( "'.urldecode(Yii::$app->urlManager->createUrl('coursedetail/lists&id=')).'"+$(this).val(), function( data ) {
$( "select#subcat_id" ).html( data );
});
']);
echo $form->field($model, 'subcat_id')
->dropDownList( $CoursesSubcat
['prompt'=>'-Choose a Sub Category-'],
['id'=>'subcat_id']
); ?>
推荐答案
public function actionLists($id)
{
echo $sql = "select * from courses_subcat where cat_id='$id' ";
$models = CoursesSubcat::findBySql($sql)->asArray()->all();
//echo "<pre>";print_r($model);exit;
if(sizeof($models) >0){
echo "<option>-Choose a Sub Category-</option>";
foreach($models as $model){
echo "<option value='".$model['id']."'>".$model['name']."</option>";
}
}
else{
echo "<option>-Choose a Sub Category-</option><option></option>";
}
}
这篇关于如何在yii2中创建依赖项下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!