我在表单中使用krajee yii2 depdrop。我按照说明操作,我的数据仍未显示在depdrop上。
这是我的_form.php代码:
<?php echo $form->field($model, 'id_trayek')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Trayek::find()->all(), 'id_trayek', 'nama_trayek'),
'language' => 'en',
'theme' => Select2::THEME_KRAJEE,
'options' => ['id' => 'id_trayek', 'placeholder' => '-- Pilih Trayek --', 'class' => 'form-control'],
]); ?>
<?php
echo $form->field($model, 'no_bus')->widget(DepDrop::classname(), [
'type'=>DepDrop::TYPE_SELECT2,
'options' => ['id'=>'no_bus', 'class'=>'form-control'],
'pluginOptions'=>[
'depends'=>['id_trayek'],
'placeholder' => '-- Pilih Bus --',
'url' => Url::to(['/surat-jalan/bus'])
]
]);
?>
这是depdrop url的控制器:
public function actionBus() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$id_trayek = end($_POST['depdrop_parents']);
$jenis_bus = Trayek::find()->select('jenis_bus')->andWhere(['id_trayek'=>$id_trayek]);
$list = Bus::find()->andWhere(['jenis_bus'=>$jenis_bus])->asArray()->all();
$selected = null;
if ($id_trayek != null && count($list) > 0) {
$selected = '';
foreach ($list as $i => $bus) {
$out[] = ['no_bus' => $bus['no_bus'], 'no_bus' => $bus['no_bus']];
}
echo Json::encode(['output' => $out, 'selected'=>$selected]);
return;
}
}
echo Json::encode(['output' => '', 'selected'=>'']);
}
在控制台中,我发现JSON响应没问题且错误:TypeError:id未定义在文件dependendent-dropdown.js上(第39行,第49行)
答案 0 :(得分:1)
这一行不正确:
$out[] = ['no_bus' => $bus['no_bus'], 'no_bus' => $bus['no_bus']];
依赖下拉列表要求第一个数组元素为id
,第二个数组元素为name
。改为:
$out[] = ['id' => $bus['no_bus'], 'name' => $bus['no_bus']];