yii2如何向dropDownList()元素添加其他属性?

时间:2015-08-09 18:38:36

标签: drop-down-menu yii2

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

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

2 个答案:

答案 0 :(得分:3)

我得到的结果请尝试这个可能解决问题我得到了击球手解决方案

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

答案 1 :(得分:2)

尝试使用选项

 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']]);,
相关问题