【问题标题】:Yii2, Set multiple Value Select2Yii2,设置多个值Select2
【发布时间】:2016-02-24 07:50:45
【问题描述】:

我有这样的加载 select2 数据:

$data = ArrayHelper::map(ContactGroups::find()->where(['group_status'=>'ACTIVE'])->asArray()->all(),'group_id', 'group_name'); 

echo $form->field($model, 'group_id')->widget(Select2::classname(), [
 'data' => $data,
 'model' => $model,
  'language' => 'en',
  'options' => ['placeholder' => Yii::t('modules','Pilih Kelompok')],
  'pluginOptions' => [
    'allowClear' => true,
    'multiple' => true,
  ],
])->label('Kelompok');

$data变量返回结果:

Array
(
    [1] => Tanpa Kategori
    [3] => Bisnis
    [4] => Kawan
    [5] => Bisnis Kerang
    [6] => Bisnis Selang
    [99] => Keluarga
)

select2 工作正常,但我无法显示所选值或initial value。我错过了什么吗?

【问题讨论】:

  • 你用过kartik-select2小部件吗?
  • 实际上,我正在使用 kartik-select2 小部件,但我从未尝试过像 $model->group_id = unserialize($model->group_id) 这样使用反序列化。
  • 查看数据库表中group_id的值

标签: php yii2 jquery-select2


【解决方案1】:

您在pluginOptions 中添加tags 属性以进行多项选择,例如......

$data = ArrayHelper::map(ContactGroups::find()->where(['group_status'=>'ACTIVE'])->asArray()->all(),'group_id', 'group_name'); 

foreach($data as $d)
     $row[]=$d;

echo $form->field($model, 'group_id')->widget(Select2::classname(), [
   'language' => 'en',
   'name' => 'group_id[]',
   'options' => ['placeholder' => ''],
   'pluginOptions' => [
        'tags' => $row,
        'allowClear' => true,
        'multiple' => true
    ],
])->label('Kelompok');

你显示Demo

【讨论】:

  • 我不知道我的代码发生了什么。但我发现了像link 这样的简单东西,它对我有用。不管怎样,谢谢大家。我会在下一个案例中尝试@vishu 代码。
  • 这是固定的@HendraSYP
  • 我在这里找到了解决方案link@YasarArafath
【解决方案2】:

尝试像这样使用..在更新时,我们需要在 1 个变量中获取已选择的值以及 1 个变量中的所有值..并将其发送到 select2。

$query = NewsTags::find()->where(['news_id' => $model->id])->all();        
 $services = array();
 $services_id_list = array();
 foreach ($query as $ds) {
 $tag_id = $ds->tag_id;
 $tag_name = Tags::findOne($tag_id)->tag_name;

 $services[$ds->tag_id] = $tag_name;
 array_push($services_id_list, $ds->tag_id);
 }
 $data= ArrayHelper::map(Tags::find()->where([])->all(),'id','tag_name');

 echo Select2::widget([
 'name' => 'NewsTags[tag_id][]',
 'id' => 'newstags-tag_id',
 'value' => $services_id_list,
 'data' => $data,
 'maintainOrder' => true,
 'options' => [
 'placeholder' => 'Select a Service ...',
 'multiple' => true
 ],
 'pluginOptions' => [
 'tags' => true,
 'maximumInputLength' => 10
 ],
 ]);   

这里的 NewsTags[tag_id][] 是模型及其列。我们不是在这里直接调用 $model->attribute

【讨论】:

    【解决方案3】:

    看看 kartik\base\InputWidget 第 190 行的代码:

    if ($this->hasModel()) {
       $this->name = !isset($this->options['name']) ? Html::getInputName($this->model, $this->attribute) : $this->options['name'];
       $this->value = !isset($this->options['value'])? Html::getAttributeValue($this->model, $this->attribute) :                                $this->options['value'];
    }
    

    我发现,使用 AJAX 加载数据时,应该在 options[value] 中设置初始多个值,如下所示:

    <?= $form->field($model, $attribute)->widget(Select2::className(), [
    'initValueText' => $initText, // array of text to show in the tag for the selected items 
    'options' => [
        'placeholder' => 'Any text you want ...',
        'multiple' => true,             
        'class' => 'form-control',
        'value' => $initIds, // array of Id of the selected items
    ],
    

    而在 initValueText 旁边设置值会导致 array_combine 错误

    【讨论】:

    • 这是我一直在寻找的答案!谢谢你救了我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 2018-05-11
    • 2014-09-14
    • 2019-02-22
    • 2017-11-10
    • 2018-05-25
    • 2019-01-14
    相关资源
    最近更新 更多