Yii2 - 向dropDownList()元素添加其他数据属性并将其选中?

时间:2017-01-17 19:43:16

标签: drop-down-menu yii2

问题已经存在,答案(链接下方),但是: 1)对我不起作用 2)不包括选择选项的额外需求

可以帮忙吗?

IM使用:

<?=$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);
 ?>

我需要,但不要:

<select name="name">

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

</select>

1 个答案:

答案 0 :(得分:1)

这里已经回答了&gt; 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);
 ?>