我有四个依赖的层次结构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>
控制器
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;
}
答案 0 :(得分:2)
之前的2个答案有一些答案,但不太清楚如何实施。
在kartik documentation page中,您有关于数据选项的文档,其中包含以下示例:
'data' => [2 => 'Music'],
您可以在控制器中设置默认值,但您没有更新的ID,只有父ID。 在视图中,您将确定模型中的ID,但并不总是具有描述的字段。
好消息是你不需要名称来显示默认的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'])
]
]);
我把默认设置放在那里,但如果id在你从控制器加载的列表中,它就可以工作。
答案 1 :(得分:1)
您是否尝试过设置选择的&#39;数组属性? 一些清理后的行动之一:
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 ]);
}
答案 2 :(得分:-1)
您需要使用预先选择的值ID添加“值”。 在你的情况下:
<?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);
?>