我使用了 Yii2 多选下拉菜单,它在创建时工作正常,但在更新时没有显示选择的值!
形式:
$form->field($model, 'categories[]')
->dropDownList($model->CategoryDropdown,
[
'multiple'=>'multiple'
'class'=>'chosen-select input-md required',
]
)->label("Add Categories");
模型:
public function getCategoryDropdown()
{
$listCategory = Category::find()->select('ID,name')
->where(['is_subcategory' => 'Yes'])
->andWhere(['status' => 'active','approved' => 'active'])
->all();
$list = ArrayHelper::map( $listCategory,'ID','name');
return $list;
}
控制器:
public function actionCreate(){
...
$model->categories = implode(",",$_POST['Company']['categories']);
...
return $this->render('create', [
'model' => $model,
]);
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
echo $model->categories; // 1,2,4,5 values already assigned
...
return $this->render('update', [
'model' => $model,
]);
}
数据库:
1,2,4,5
更新记录时如何在下拉列表中显示多选值?