How To Select Multiple Value In Dropdownlist?

How to select multiple value in dropdownlist?

like this




echo $form->dropDownList($model, 'surgeonId', $surgeonList, array('prompt' => 'Select', 'multiple' => true, 'selected' => 'selected'));



1 Like

ya its right …but i want to more than value selected in dropdown list like combo-box

i m using below this code

echo Select2::widget([

      'name' => 'topic_id', 


      'model' => $model,


      'data' =>  ArrayHelper::map(Topics::find()->orderBy('title')->asArray()->all(), 'id', 'title'),


     


      'options' => [


      'placeholder' => 'Select Topics ...', 


      'data' =>'$model->topic_id',


      'class'=>'form-control', 


      'multiple' => true


    ],


  ]);

this code is proper work but

multiple value are not selected for edit mode…

so whant can i change in this code.

what format you are using to save selected id’s in table ?

comma separated like 1,2,3

then it should work




echo $form->dropDownList($model, 'surgeonId', $surgeonList, array('prompt' => 'Select', 'multiple' => true, 'selected' => 'selected'));



if you are using as above instead of widget (its worrking for me)

ok i will try

$surgeonList= ArrayHelper::map(Topics::find()->orderBy(‘title’)->asArray()->all(), ‘id’, ‘title’);

echo $form->field($model, 'topic_id')->dropDownList(  $model, 'topic_id', $surgeonList, array('prompt' => 'Select', 'multiple' => true, 'selected' => 'selected'));

m use your code so… display this error

array_merge(): Argument #2 is not an array

m modify this code showing here…

so error solved

echo $form->field($model, ‘topic_id’)->dropDownList(

 ArrayHelper::map(Topics::find()->orderBy('title')->asArray()->all(), 'id', 'title'),


  ['prompt' => 'Select topics', 'multiple' => true, 'selected' => 'selected']


  


  );

but problem one…

one value selected on edit time when one value is inserted for insert time…

but two or more value are not selected on edit time when two or more values are inserted for insert time.

are you using 2.0 version ? it seems by seeing this




echo $form->field($model, 'topic_id')->dropDownList(

ArrayHelper::map(Topics::find()->orderBy('title')->asArray()->all(), 'id', 'title'),

['prompt' => 'Select topics', 'multiple' => true, 'selected' => 'selected']


);




above I use in 1.1 only

yes m using 2.0 version .

Thank u give me guide.

so any other idea?

on an other note this should be "attribute" not "name"


<?php

echo Select2::widget([

	// 'name' => 'topic_id', 

	'attribute' => 'topic_id', 

	'model' => $model,

	'data' => ArrayHelper::map(Topics::find()->orderBy('title')->asArray()->all(), 'id', 'title'),


	'options' => [

		'placeholder' => 'Select Topics ...', 

		'data' =>'$model->topic_id',

		'class'=>'form-control', 

		'multiple' => true

	],

]);

?>

thank u so much its work :D

i m adding ‘attribute’ =>‘topic_id’,

one another problem create

Edit time value has been selected,

but does not insert the form.

and not create new record.

So please help me

Super old, but for anyone else, you need to call SomeJunctionTable::deleteAll(['some_junction_id' => $model->id]); Before repopulating with the new ID’s.