1
創建依賴下拉我使用此代碼對backend/views/_form.php
如何yii2
<?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']
); ?>
請給'CoursesSubcat'表結構 – vishuB
此鏈接有助於您http://www.yiiframework.com/wiki/723/creating-a-dependent-dropdown-from-scratch-in-yii2/ – jithin