Konsultan IT Bandung
Konsultan IT Bandung

Reputation: 151

Yii2, Set multiple Value Select2

I have load select2 data like this :

$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 variable returning result :

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

and select2 working properly, but I can't show selected value or initial value. is I've missed something ?

Upvotes: 0

Views: 20459

Answers (3)

Salman Riyaz
Salman Riyaz

Reputation: 818

Try using like this.. At the time of updating we need to take already selected values in 1 variable and all values in 1 variable.. and send this to 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
 ],
 ]);   

here NewsTags[tag_id][] is the model and its column. we are not directly calling $model->attribute here

Upvotes: 2

dom
dom

Reputation: 87

Having a look at the code of kartik\base\InputWidget line 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'];
}

I've found out that, when loading data with AJAX, initial multiple values should be set in options[value] like this:

<?= $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
],

whereas setting value next to initValueText leads to an array_combine error

Upvotes: 1

vishuB
vishuB

Reputation: 4261

you add tags property in pluginOptions for multiple selection like....

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

You show Demo

Upvotes: 2

Related Questions