yii2 如何向 dropDownList() 元素添加额外的属性?

yii2 how to add additional attributes to dropDownList() elements?

问题很简单。我想为每个选择选项添加不同的数据属性,以便 Bootstrap 将根据选择的选项加载不同的数据。

我应该怎么处理它?向 field()->dropDownList() 添加选项只会将选项添加到列表元素容器。

试试选项

 echo $form->field($model, 'name')->dropDownList( $listData,
                                ['options'=>['class' => 'yourClass', 
                                 'style' => 'yourStyle', 
                                  'yourAtt'=> 'yourattribute']]);

或尝试

echo $form->field($model, 'name')->dropDownList( $listData,
                                ['data-target'=> 'yourValue',
                                 'data-toggle' => ' your2value']);

您可以使用的项目

echo $form->field($model, 'name')->dropDownList( $listData,
                                ['options'=>[
                                   'value1' => ['data-target' => 'yourAtt']]);,

我得到了结果请试试这个可能会解决问题我得到了连击解决方案

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