Нашел пример как реализовать зависимые списки на yii2 с помощью виджета
demos.krajee.com/widget-details/depdrop
Попытался использовать данный пример. выдает ошибку
POST localhost/zakaz/service 400 (Bad Request)
view:
$data = ArrayHelper::map(Service::find()->all(), 'id', 'title');
<?= // Tagging support Single
$form->field($model, 'description', ['options' => ['class' => 'col-sm-6']])->widget(Select2::classname(),[
'name' => 'service',
'data' =>$data,
'language' => 'ru',
'initValueText' => ['1'],
'options' => [
'placeholder' => 'Select a service ...',
],
'pluginOptions' => [
'allowClear' => false,
'tags' => true,
'tokenSeparators' => [',', ' '],
'maximumInputLength' => 10
],
])->label(false) ?>
<?= $form->field($model, 'service_params', ['options' => ['class' => 'col-sm-6']])->widget(DepDrop::classname(), [
'type'=>DepDrop::TYPE_SELECT2,
'pluginOptions'=>[
'depends'=>['zakaz-description'],
'placeholder' => 'Выберите категорию...',
'url' => Url::to(['/zakaz/service']),
'loadingText' => 'Загрузка категорий...',
]
])->label(false); ?>
Controller:
public function actionService()
{
$out = [];
if (isset($_POST['depdrop_parents'])) {
$id_service = end($_POST['depdrop_parents']);
$list = ServiceParameters::findAll(['description'=>$id_service]);
$selected = null;
if ($id_service != null && count($list) > 0) {
$selected = '';
foreach ($list as $i => $model) {
$zakaz = Zakaz::findOne($model['id_zakaz']);
$category = User::findOne($model['id']);
$out[] = ['id' => $model['id'], 'name' => $zakaz->description.' ('.$category->username.')'];
if ($i == 0) {
$selected = $model['id'];
}
}
// Shows how you can preselect a value
echo Json::encode(['output' => $out, 'selected'=>$selected]);
return;
}
}
echo Json::encode(['output' => '', 'selected'=>'']);
}
Если перейти на страницу
localhost/zakaz/service
То выдает
{"output":"","selected":""}