Yii2, Kartik DepDrop на предварительно выбранном значении обновления

У меня есть четыре зависимых раскрывающихся списка иерархической library > catalog > category > subject

Я использую kartik depdrop , все работает отлично, но при обновлении я не смог найти решение для предварительно выбранных значений. Мне нужно показать предварительно выбранные значения при обновлении со всеми другими значениями, чтобы пользователь мог также изменить выбор.

Вот мой код.

_form.php

 <div class="inner-addon right-addon"> <div class="catalog-input"> <span><strong>Library</strong></span> <?php echo $form->field($modelDep, 'lesson_name[0]')->dropDownList($libList, [ 'prompt' => 'Select Library', 'id'=>'lib-id-1', 'name'=>'Lesson[library]' ])->label(false); ?> </div> <div class="category-input"> <span><strong>Catalog</strong></span> <?php echo $form->field($modelDep, 'lesson_name[1]')->widget(DepDrop::classname(), [ 'options'=>['id'=>'catalog-id-1', 'name'=>'Lesson[catalog]'], 'pluginOptions'=>[ 'depends'=>['lib-id-1'], 'placeholder'=>'Select Catalog', 'url'=>Url::to(['/lesson/catalog']) ] ])->label(false); ?> </div> <div class="subject-input"> <span><strong>Category</strong></span> <?php echo $form->field($modelDep, 'lesson_name[2]')->widget(DepDrop::classname(), [ 'options'=>['id'=>'cat-id-1', 'name'=>'Lesson[category]'], 'pluginOptions'=>[ 'depends'=>['catalog-id-1'], 'placeholder'=>'Select Category', 'url'=>Url::to(['/lesson/category']) ] ])->label(false); ?> </div> <div class="topic-input"> <span><strong>Subject</strong></span> <?php echo $form->field($modelDep, 'lesson_name[3]')->widget(DepDrop::classname(), [ 'options'=>['id'=>'subject-id-1', 'name'=>'Lesson[fk_subject]'], 'pluginOptions'=>[ 'depends'=>['cat-id-1'], 'placeholder'=>'Select Subject', 'url'=>Url::to(['/lesson/subject']) ] ])->label(false); ?> </div> </div> от <div class="inner-addon right-addon"> <div class="catalog-input"> <span><strong>Library</strong></span> <?php echo $form->field($modelDep, 'lesson_name[0]')->dropDownList($libList, [ 'prompt' => 'Select Library', 'id'=>'lib-id-1', 'name'=>'Lesson[library]' ])->label(false); ?> </div> <div class="category-input"> <span><strong>Catalog</strong></span> <?php echo $form->field($modelDep, 'lesson_name[1]')->widget(DepDrop::classname(), [ 'options'=>['id'=>'catalog-id-1', 'name'=>'Lesson[catalog]'], 'pluginOptions'=>[ 'depends'=>['lib-id-1'], 'placeholder'=>'Select Catalog', 'url'=>Url::to(['/lesson/catalog']) ] ])->label(false); ?> </div> <div class="subject-input"> <span><strong>Category</strong></span> <?php echo $form->field($modelDep, 'lesson_name[2]')->widget(DepDrop::classname(), [ 'options'=>['id'=>'cat-id-1', 'name'=>'Lesson[category]'], 'pluginOptions'=>[ 'depends'=>['catalog-id-1'], 'placeholder'=>'Select Category', 'url'=>Url::to(['/lesson/category']) ] ])->label(false); ?> </div> <div class="topic-input"> <span><strong>Subject</strong></span> <?php echo $form->field($modelDep, 'lesson_name[3]')->widget(DepDrop::classname(), [ 'options'=>['id'=>'subject-id-1', 'name'=>'Lesson[fk_subject]'], 'pluginOptions'=>[ 'depends'=>['cat-id-1'], 'placeholder'=>'Select Subject', 'url'=>Url::to(['/lesson/subject']) ] ])->label(false); ?> </div> </div> от <div class="inner-addon right-addon"> <div class="catalog-input"> <span><strong>Library</strong></span> <?php echo $form->field($modelDep, 'lesson_name[0]')->dropDownList($libList, [ 'prompt' => 'Select Library', 'id'=>'lib-id-1', 'name'=>'Lesson[library]' ])->label(false); ?> </div> <div class="category-input"> <span><strong>Catalog</strong></span> <?php echo $form->field($modelDep, 'lesson_name[1]')->widget(DepDrop::classname(), [ 'options'=>['id'=>'catalog-id-1', 'name'=>'Lesson[catalog]'], 'pluginOptions'=>[ 'depends'=>['lib-id-1'], 'placeholder'=>'Select Catalog', 'url'=>Url::to(['/lesson/catalog']) ] ])->label(false); ?> </div> <div class="subject-input"> <span><strong>Category</strong></span> <?php echo $form->field($modelDep, 'lesson_name[2]')->widget(DepDrop::classname(), [ 'options'=>['id'=>'cat-id-1', 'name'=>'Lesson[category]'], 'pluginOptions'=>[ 'depends'=>['catalog-id-1'], 'placeholder'=>'Select Category', 'url'=>Url::to(['/lesson/category']) ] ])->label(false); ?> </div> <div class="topic-input"> <span><strong>Subject</strong></span> <?php echo $form->field($modelDep, 'lesson_name[3]')->widget(DepDrop::classname(), [ 'options'=>['id'=>'subject-id-1', 'name'=>'Lesson[fk_subject]'], 'pluginOptions'=>[ 'depends'=>['cat-id-1'], 'placeholder'=>'Select Subject', 'url'=>Url::to(['/lesson/subject']) ] ])->label(false); ?> </div> </div> 

контроллер

 public function actionCatalog() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $lib_id = $parents[0]; $out = $this::getCatalogList($lib_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getCatalogList($lib_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Catalog::find() ->where(['fk_library'=>$lib_id,'fk_company'=>$company->id]) ->select(['id','catalog_name AS name' ])->asArray()->all(); return $data; } public function actionCategory() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $cat_id = $parents[0]; $out = $this::getCategoryList($cat_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getCategoryList($cat_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Category::find() ->where(['fk_catalog'=>$cat_id,'fk_company'=>$company->id]) ->select(['id','category_name AS name' ])->asArray()->all(); return $data; } public function actionSubject() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $category_id = $parents[0]; $out = $this::getSubjectList($category_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getSubjectList($category_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Subject::find() ->where(['fk_category'=>$category_id,'fk_company'=>$company->id]) ->select(['id','subject_name AS name' ])->asArray()->all(); return $data; } - public function actionCatalog() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $lib_id = $parents[0]; $out = $this::getCatalogList($lib_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getCatalogList($lib_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Catalog::find() ->where(['fk_library'=>$lib_id,'fk_company'=>$company->id]) ->select(['id','catalog_name AS name' ])->asArray()->all(); return $data; } public function actionCategory() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $cat_id = $parents[0]; $out = $this::getCategoryList($cat_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getCategoryList($cat_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Category::find() ->where(['fk_catalog'=>$cat_id,'fk_company'=>$company->id]) ->select(['id','category_name AS name' ])->asArray()->all(); return $data; } public function actionSubject() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $category_id = $parents[0]; $out = $this::getSubjectList($category_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getSubjectList($category_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Subject::find() ->where(['fk_category'=>$category_id,'fk_company'=>$company->id]) ->select(['id','subject_name AS name' ])->asArray()->all(); return $data; } - public function actionCatalog() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $lib_id = $parents[0]; $out = $this::getCatalogList($lib_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getCatalogList($lib_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Catalog::find() ->where(['fk_library'=>$lib_id,'fk_company'=>$company->id]) ->select(['id','catalog_name AS name' ])->asArray()->all(); return $data; } public function actionCategory() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $cat_id = $parents[0]; $out = $this::getCategoryList($cat_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getCategoryList($cat_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Category::find() ->where(['fk_catalog'=>$cat_id,'fk_company'=>$company->id]) ->select(['id','category_name AS name' ])->asArray()->all(); return $data; } public function actionSubject() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $category_id = $parents[0]; $out = $this::getSubjectList($category_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getSubjectList($category_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Subject::find() ->where(['fk_category'=>$category_id,'fk_company'=>$company->id]) ->select(['id','subject_name AS name' ])->asArray()->all(); return $data; } - public function actionCatalog() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $lib_id = $parents[0]; $out = $this::getCatalogList($lib_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getCatalogList($lib_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Catalog::find() ->where(['fk_library'=>$lib_id,'fk_company'=>$company->id]) ->select(['id','catalog_name AS name' ])->asArray()->all(); return $data; } public function actionCategory() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $cat_id = $parents[0]; $out = $this::getCategoryList($cat_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getCategoryList($cat_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Category::find() ->where(['fk_catalog'=>$cat_id,'fk_company'=>$company->id]) ->select(['id','category_name AS name' ])->asArray()->all(); return $data; } public function actionSubject() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $category_id = $parents[0]; $out = $this::getSubjectList($category_id); echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } public static function getSubjectList($category_id){ $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST']))); $company = \common\models\Company::find()->where(['company_name' => $company_name])->one(); $data= \common\models\Subject::find() ->where(['fk_category'=>$category_id,'fk_company'=>$company->id]) ->select(['id','subject_name AS name' ])->asArray()->all(); return $data; } 

Вы пытались установить атрибут «selected»? Одно из ваших действий после некоторой очистки:

 public function actionSubject() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $category_id = $parents[0]; $out = $this::getSubjectList($category_id); $pre_selected_subject_id = 5; // or whatever you want to be default } } echo Json::encode(['output'=>$out, 'selected'=>$pre_selected_subject_id ]); } 

В двух предыдущих ответах есть немного ответа, но не слишком ясно, как реализовать.

На странице документации kartik у вас есть документация о параметре данных со следующим примером:

 'data' => [2 => 'Music'], 

Вы можете установить значение по умолчанию в своем контроллере, но у вас нет обновленного id там только родительский идентификатор. В представлении вы обязательно найдете идентификатор в своей модели, но не всегда поле с описанием.

Хорошо, что вам не нужно имя, чтобы показывать по умолчанию только идентификатор, например:

  $panelBookRights .= $form->field($model, 'primary_agent_id')->widget(DepDrop::classname(), [ 'data' => [$model->primary_agent_id => 'default'], 'options' => ['id' => 'primary-agent-id'], 'pluginOptions' => [ 'depends' => ['book-rights_owner_id'], 'initialize' => true, 'placeholder' => 'Select...', 'url' => Url::to(['contact/list_primaryagents']) ] ]); 

Я помещаю по умолчанию, но он работает, если идентификатор находится в списке, который вы загружаете с контроллера.

Вам нужно было добавить «значение» с предварительно выбранным идентификатором значения. В твоем случае:

 <?php echo $form->field($modelDep, 'lesson_name[2]') ->widget(DepDrop::classname(), [ 'options'=>['id'=>'cat-id-1', 'name'=>'Lesson[category]', 'value' => $preSelectId, ], 'pluginOptions'=>[ 'depends'=>['catalog-id-1'], 'placeholder'=>'Select Category', 'url'=>Url::to(['/lesson/category']) ] ])->label(false); ?> от <?php echo $form->field($modelDep, 'lesson_name[2]') ->widget(DepDrop::classname(), [ 'options'=>['id'=>'cat-id-1', 'name'=>'Lesson[category]', 'value' => $preSelectId, ], 'pluginOptions'=>[ 'depends'=>['catalog-id-1'], 'placeholder'=>'Select Category', 'url'=>Url::to(['/lesson/category']) ] ])->label(false); ?>