Teerath Kumar Teerath Kumar - 1 month ago 15
PHP Question

Multiple selected values in ESelect2 dropdown list using Yii ESelect2 Extenstion

I am using this in form:

$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
is an array, I want this array as selected in data, when i move to update the data, like the dropdown attribute
selected="selected"

Answer

Select2 is smart to populate itself with predefined values. But these must be inside the model attribute. If it's multiple the $model->Employee should be an array:

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

moreover the key=>value in here should be mapped in the same way as the Employee model data.

Yet the 'data' parameter still should contain all the data in the range.

So try this, where Employee should be the model containing all the data options:

Updated

I've added tags in options as shown in here(usage 1st example) and here.

 $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'
   ),
));