Friday, July 08, 2016

Yii - Give attribute values to each option in dropDownList

How can we give some attribute values to each option. For Example I need to build a dropdownlist looks like below.

<select>
    <option value="a" myAttribute="xyz">Calendar</option>
    <option value="n" myAttribute="PQR">Shopping Cart</option>
    <option value="c" myAttribute="ABC">CD</option>
    <option value="d" myAttribute="HMN">Email</option>  
</select>

values and myAttributes's value are coming from table. How can I implement this by altering the following?


Yeah have a solution to implement..Here the below solution.

In View:

<?php
     echo $form->dropDownList($model,'media_ids',
               'data' => CHtml::listData(Media::model()->findAll(), 'id', 'name'),
                array(
                     'options' =>  CHtml::listData(Media::model()->findAll(), 'id', 'option_array')
                     )
     );
?>

In Model:

public function getOption_array() {
        return array('myAttribute' => $this->media_title);
    }


Cheers !!!

No comments:

Post a Comment