0

Question already exists with answer (below link), but: 1) doesn't work for me 2) doesn't include the added need to have a selected option

Can help?

IM USING:

<?=$form->field($invoice, 'id')
        ->dropDownList(ArrayHelper::map($some_items_array, 'value_field', 'show_field'), [
            'options' => [
                 $some_selected_id => ['Selected'=>true]],
                 'data' => ['attrib1' => "valueA', 'attrib2' => "valueB']
             'class' => 'form-control',
             'prompt' => ''])->label(false);
 ?>

I NEED, BUT DO NOT GET:

<select name="name">

<option value="value" data-attrib1="valueA" data-attrib2="valueB">text< option>

</select>

1 Answer 1

1

Already answered here > YII - Add another attribute to dropDownList

$attributes = [
    'attrib1' => 'valueA',
    'attrib2' => 'valueB',
];

foreach ($some_items_array as $index => $att) {
    $dropdownlist_options[$index] = $attributes;
}

<?=$form->field($invoice, 'id')
    ->dropDownList(ArrayHelper::map($some_items_array, 'value_field', 'show_field'), [
        'options' => $dropdownlist_options, /* [
             $some_selected_id => [
                 'selected' => true,
                 'attrib1' => 'valueA',
                 'attrib2' => 'valueB',
             ],
             $some_other_id => [
                 'attrib1' => 'valueA',
                 'attrib2' => 'valueB',
             ],

         ],*/
         'class' => 'form-control',
         'prompt' => '',
     ])->label(false);
 ?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. The problem with this is that this presupposes that all of the option value id's are known and finite (in order to be able to declare each). $some_items_array returns a list of items who's length and values are unknown before runtime.
Ahh.. yes, that worked, thanks!! I had to programitically select a value so used this slightly modified line: 'options' => [$some_selected_id => ['Selected'=>true], $dropdownlist_options].Thanks again!!
actually adding the $some_selected_id line to the options didn't work. I'll prob wind up setting the selected value through jQuery.

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.