yii2 how to add additional attributes to dropDownL

2019-06-26 08:29发布

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.

2条回答
对你真心纯属浪费
2楼-- · 2019-06-26 08:56

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']]);,
查看更多
叼着烟拽天下
3楼-- · 2019-06-26 09:10

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);
查看更多
登录 后发表回答