yii -> dropDownList -> CHtml::listData -> set selected value

user2227400 picture user2227400 · Nov 12, 2013 · Viewed 24k times · Source

How can I set the selected value in the following statement:

echo $form->dropDownList(
    $model,
    'land_id',
    CHtml::listData(ListeLand::model()->findAll(), 'iso', 'landname'),
    array('class'=>'span4 chosen','maxlength'=>20)
);

I want to set in the list:

iso = AT
landname = Österreich

as selected value.

EDITED -> The solution:

echo $form->dropDownList(
    $model,
    'land_id',
    CHtml::listData(ListeLand::model()->findAll(), 'iso', 'landname'),
    array(
                'class'=>'span4 chosen',
                'maxlength'=>20,
                'options' => array('AT'=>array('selected'=>true)),
    )
);

Answer

user2227400 picture user2227400 · Nov 13, 2013

this works..

echo $form->dropDownList(
    $model,
    'land_id',
    CHtml::listData(ListeLand::model()->findAll(), 'iso', 'landname'),
    array(
                'class'=>'span4 chosen',
                'maxlength'=>20,
                'options' => array('AT'=>array('selected'=>true)),
    )
);