【问题标题】:Multiple selected values in ESelect2 dropdown list using Yii ESelect2 Extenstion使用 Yii Select2 Extension 在 Select2 下拉列表中选择多个值
【发布时间】:2014-12-19 11:29:31
【问题描述】:

我在表单中使用这个:

$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"

【问题讨论】:

    标签: php yii


    【解决方案1】:

    Select2 很聪明地用预定义的值填充自己。但这些必须在模型属性内。如果是多个 $model->Employee 应该是一个数组:

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

    此外,这里的key=>value 应该以与Employee 模型数据相同的方式映射。

    但“数据”参数仍应包含该范围内的所有数据。

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

    更新

    我在选项中添加了标签,如here(usage 1st example)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'
       ),
    ));
    

    【讨论】:

    • $model->Employee 是一个数组。但是这个数组在这个下拉选项中带来了数据,但不显示为选定的元素。
    • 但无法正常工作,并且 tags 元素将其形状更改为经典。
    • 它只是建议数据,而不是显示为选定的值。
    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 2021-05-23
    • 2020-02-25
    相关资源
    最近更新 更多