YII - Add another attribute to dropDownList

两盒软妹~` 提交于 2020-01-13 09:24:09

问题


I will to add another attribute to dropDownList.

I will a dropdown list like this with Yii dropDownList:

<select name="city" id="city">
    <option value="1" test="123">one</option>
    <option value="2" test="234">two</option>
    <option value="3" test="345">three</option>
    <option value="4" test="456">four</option>
</select>

I will add test attribute to option tags.

Default Yii dropDownList is:

<?php 

echo CHtml::activeDropDownList('City', 'City', array(1 => 'one', 2 => 'two')); 

?>

How I can do this?


回答1:


Try :

<?php 
echo CHtml::dropDownList(
    'City',
    'City',
    array(1 => 'one', 2 => 'two'),
    array('options' => array(
        '1' => array('test' => '123'),
        '2' => array('test' => '234'),
    ))
);
?>



回答2:


Same thing but in Yii2

echo $form->field($model, 'id_type_question')->dropDownList(
    ArrayHelper::map(
        YourModel::find()->all(), 
        'option_id', 
        'option_value'
    ), 
    [
        'options' => ArrayHelper::map($models, 
            'id', 
            function ($m) { return [ 'your_tag' => $m['your_column'] ]; }
        )
    ]
); 


来源:https://stackoverflow.com/questions/19861424/yii-add-another-attribute-to-dropdownlist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!