Wednesday, January 27, 2016

How to make default value get selected in yii dropdown list

in controller

$resourceperson = ResourcePerson::model ()->findAll ();
$resourceperson = CHtml::listData ( $resourceperson, 'id', 'name' );

$this->render ( 'update', array (
'model' => $model,
'resourceperson' => $resourceperson
) );

in _form.php

<div class="row">
<?php echo $form->labelEx($model,'resource_person_id'); ?>

<?php
  //echo $form->textField($model,'resource_person_id');
/* echo $form->dropDownlist($model,'resource_person_id',$resourceperson,
array(
'id'=>'resource_person_id',
'multiple' => 'multiple',
'style'=>'width:130px;',
'prompt'=>'Please Select Resource Person'
)
); */
$selectedOptions = "";
if ($model->resource_person_id != null) {
$resourcePersons = explode(",", $model->resource_person_id);
foreach ($resourcePersons as $eachValue){
$selectedOptions[$eachValue] = array('selected'=>true);
}
}
echo $form->dropDownList($model,'resource_person_id',$resourceperson,
array(
'id'=>'resource_person_id',
'multiple' => 'multiple',
'style'=>'width:130px;',
'options' => $selectedOptions
)
);
?>

<?php echo $form->error($model,'resource_person_id'); ?>
</div>