Здравствуйте! Помогите, пожалуйста, разобраться с depdrop scenario 3 от kartik.
Есть 2 таблицы: Executor (id, executor) и Directory (id, directory, id_executor)
Делаю по документации kartik по итогу выпадающие списки получаются независимые друг от друга.
// THE VIEW
// Top most parent
<?= $form->field($model, 'id_executor')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Executor::find()->groupBy('executor')->asArray()->all(), 'id', 'executor'), 'options' => ['placeholder' => 'Выберите исполнителя ...']]);?>
// Child
<?= $form->field($model, 'directory')->widget(DepDrop::classname(), [
'data' => ArrayHelper::map(Directory::find()->groupBy('directory')->asArray()->all(),'id', 'directory'),
'options' => ['placeholder' => 'Выберите должника ...'],
'type' => DepDrop::TYPE_SELECT2,
'select2Options' => ['pluginOptions' => ['allowClear' => true]],
'pluginOptions' => [
'depends' => ['select2-directory-id_executor-container'],
'url' => Url::to(['/directory/directory']),
'loadingText' => 'Загрузка ...',
]
]);?>
// CONTROLLER
public function actionChildAccount() {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out = [];
if (isset($_POST['depdrop_parents'])) {
$id = end($_POST['depdrop_parents']);
$list = Directory::find()->andWhere(['id_executor' => $id])->asArray()->all();
$selected = null;
if ($id != null && count($list) > 0) {
$selected = '';
foreach ($list as $i => $model) {
$out[] = ['id' => $model['id'], 'executor' => $model['executor']];
if ($i == 0) {
$selected = $model['id'];
}
}
// Shows how you can preselect a value
return ['output' => $out, 'selected' => $selected];
}
}
return ['output' => '', 'selected' => ''];
}