我想在Yii2中创建一个dependend dropDownList。我正在尝试使用DepDrop Widget,但我无法理解如何根据自己的情况编辑代码。我有1个模型,里面我需要做类别下拉列表,根据category_id,下一个dropDownList应该是Item。 (如果我选择类别1,该项目应该是Item1等等)。如何在Yii2中创建相关的下拉列表?
我想扩展只能做同一模型的下拉?我是Yii2的新手,所以。
我view
文件:
<div class="site-create">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'code') ?>
<?= $form->field($category, 'id')->dropDownList($category, ['id'=>'category-id']); ?>
<?= $form->field($item, 'subcat')->widget(DepDrop::Item(), [
'options'=>['id'=>'item-id'],
'pluginOptions'=>[
'depends'=>['category-id'],
'placeholder'=>'Select...',
'url'=>Url::to(['/site/subcat'])
]
]); ?>
我$model
为$category
和$item
不同的模式。我把这些变量在行动
这是我action
使用不同的模式:
public function actionSubcat() {
$category = new Category();
$item = new Item();
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = self::getSubCatList($cat_id);
echo Json::encode(['output'=>$out, 'selected'=>'']);
return $this->render('create', [
'category' => $category,
'item' => $item,
]);
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
}
现在我得到的错误信息,即$category
变量是不确定的。有人能解释我做错了什么吗?