Answer the question
In order to leave comments, you need to log in
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' =>[ ??? ]
]
); ?>
<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
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 questionAsk a Question
731 491 924 answers to any question