Yii2下拉多个选定值 [英] Yii2 dropdown multiple selected values
本文介绍了Yii2下拉多个选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用了Yii2多项选择下拉菜单,它在创建时工作正常,但在更新时没有显示所选值!
I used Yii2 multiple selection dropdown , it is working fine at create but not showing me selected values on update!
表格:
$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
更新我的重新记录后,如何在下拉列表中显示多个选定的值?
How I can show multi selected values in dropdown when I update my recored?
推荐答案
您所有的代码都可以,只需要echo
您的$ form-> ...
all your code is ok just need echo
your $form->...
echo $form->field($model, 'categories[]')
->dropDownList($model->CategoryDropdown,
[
'multiple'=>'multiple',
'class'=>'chosen-select input-md required',
]
)->label("Add Categories");
或在视图中使用<?= ?>
!
这篇关于Yii2下拉多个选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文