M
M
Monitorkin2016-03-15 16:23:12
Yii
Monitorkin, 2016-03-15 16:23:12

How to add data-attribute to each item of a DropDownList dropdown (Yii2)?

I'm asking because I haven't found a normal working example anywhere.
There is a DropDownList dropdown list:
From the ObjectType model, you need to use 'object_type' and 'type_attr'.

<?= $form->field($model, 'type')->dropDownList(ArrayHelper::map(ObjectType::find()->all(), 'object_type', 'object_type'),
    [  'prompt' => 'Выберите тип объекта..',
       'options' =>[ ??? ]
    ]
); ?>

What needs to be added to 'options' to make it look like this:
<select>
<option value="type1" data-attribute="type_attr1">type1</option>
     ...
<option value="type2" data-attribute="type_attr2">type2</option>
</select>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-03-15
@Monitorkin

Everything is in the dock . More or less like this.

<?php 
    $attrs = ArrayHelper::map(ObjectType::find()->all(), 'object_type', 
        function($t){
            return ['data-attribute' => $t['type_attr']];
        });
?>
<?= $form->field($model, 'type')->dropDownList(ArrayHelper::map(ObjectType::find()->all(), 'object_type', 'object_type'),
    [  
       'prompt' => 'Выберите тип объекта..',
       'options' => $attrs;
    ]
); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question