Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2 how to add additional attributes to dropDownList() elements?

The question is simple. I want to add different data-attributes to each selection option so that Bootstrap will load different data according to which option was selected.

How should I go with it? Adding options to field()->dropDownList() will only add the options to list elements container.

like image 688
jeesus Avatar asked Dec 25 '22 16:12

jeesus


2 Answers

I got the result please try this one may be solve problem i got batter solution

$listOption           = [
                                [156] => 'Option1',
                                [157] => 'Option2',
                                [158] => 'Option3',
                                [162] => 'Option4'
                            ];


$optionDataAttributes = [
                                [156] => [
                                    ['data-width']  => 10,
                                    ['data-height'] => 10
                                ],
                                [157] => [
                                    ['data-width']  => 11,
                                    ['data-height'] => 11
                                ],
                                [158] => [
                                    ['data-width']  => 12,
                                    ['data-height'] => 12
                                ],
                                [162] => [
                                    ['data-width']  => 13,
                                    ['data-height'] => 13
                                ],
                            ];



echo $form->field($model, 'field_name')->dropDownList($listOption , 
    [ 
    'options' => $optionDataAttributes, 
    'prompt' => 'Select Custom Package', 
    'class' => 'form-control input-sm', 
    'id' => 'package_select_id'
    ])->label(false);
like image 173
Chirag Pipariya Avatar answered May 25 '23 08:05

Chirag Pipariya


Try with options

 echo $form->field($model, 'name')->dropDownList( $listData,
                                ['options'=>['class' => 'yourClass', 
                                 'style' => 'yourStyle', 
                                  'yourAtt'=> 'yourattribute']]);

or try

echo $form->field($model, 'name')->dropDownList( $listData,
                                ['data-target'=> 'yourValue',
                                 'data-toggle' => ' your2value']);

for items you can use

echo $form->field($model, 'name')->dropDownList( $listData,
                                ['options'=>[
                                   'value1' => ['data-target' => 'yourAtt']]);,
like image 20
ScaisEdge Avatar answered May 25 '23 09:05

ScaisEdge