ESelect2 下拉列表中的多个选定值使用 Yii ESelect2 扩展


Multiple selected values in ESelect2 dropdown list using Yii ESelect2 Extenstion

我在形式上使用它:

$this->widget('ext.select2.ESelect2',array(
'model'=>$model,
  'attribute'=>'Employee',
  'data'=>$model->Employee,
  'options'=>array(
    'width'=>'210','placeholder'=>'All Offices',
  ),
   'htmlOptions' => array(
'multiple' => 'multiple'
),
));

$model->Employee是一个数组,我希望在数据中选择这个数组,当我移动以更新数据时,就像下拉列表属性selected="selected"

Select2 很聪明地使用预定义的值填充自身。但这些必须在模型属性中。如果是多个,则$model->Employee应为数组:

 $model->Employee = array(1=>'Allen', 2=>'John'); // or similar

此外,此处的key=>value应以与员工模型数据相同的方式映射。

然而,"data"参数仍然应该包含范围中的所有数据。

所以试试这个,其中Employee应该是包含所有数据选项的模型:

更新

我在选项中添加了标签,如下所示(用法第一个示例(和此处。

 $tags= array(1=>'Allen', 2=>'John');
 $this->widget('ext.select2.ESelect2',array(
   'model'=>$model,
   'attribute'=>'Employee',
   'data'=> Chtml::listData(Employee::model()->findAll(), 'id', 'name'),
   'options'=>array(
    'width'=>'210','placeholder'=>'All Offices',
    'tags'=>$tags, 
   ),
   'htmlOptions' => array(
   'multiple' => 'multiple'
   ),
));