4

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 Answers 2

8

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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']]);,

7 Comments

What kind of attributes you want to add? show a sample
I want to add data-target and data-toggle attributes to trigger various Bootstrap events. But even simple class nor style attributes won't render.
Yes, but as I stated, my goal is to assign these values for every dropdown list element. This solution assigns these values to <select>. I want it to be like: <select id="article" class="form-control" name="Articles[article_type]"> <option value="1" data-target="news">News</option> <option value="2" data-target="blog">Blog</option> <option value="3" data-target="gossip">Gossip</option> </select>
Yeah, I ended up with the same solution myself also. But thank you a lot!
That's not a complete answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.