kkozlowski
(Kkozlowski89)
November 13, 2010, 2:50pm
1
Hi everyone,
I’m sorry for my english:) I have problem with a dropDownList and default value.
When I use this code:
<?php echo CHtml::dropDownList('name', '0', array('0'=>'value1', '1'=>'value2'));
For this example dafault value is ‘value1’. It’s ok but when I want to use dropDownList and $model I can’t to select default value. See this code, please.
<?php echo CHtml::dropDownList($model, 'attrubuteName, $data)); ?>
I don’t know where to give a default value. Has anyone any idea? Is it possible?
Thanks.
jzu
(Tiger Jackson)
November 13, 2010, 3:18pm
2
If you want to use the dropDownList with model, you should set the default value in either controller or model class
e.g.
//inside the controller's action function
$model->name = 0;
$this->render(..........
//in the model class declaration
$name = 0;
jacmoe
(Jacob Moena)
November 13, 2010, 4:36pm
3
You don’t have to do that.
I think this would work:
<?php echo CHtml::dropDownList('name', '0', array('0'=>'value1', '1'=>'value2'),
array('options' => array('1'=>array('selected'=>true)))); ?>
jzu
(Tiger Jackson)
November 13, 2010, 4:44pm
4
jacmoe:
You don’t have to do that.
I think this would work:
<?php echo CHtml::dropDownList('name', '0', array('0'=>'value1', '1'=>'value2'),
array('options' => array('1'=>array('selected'=>true)))); ?>
Isn’t he want to use the model method??
kkozlowski
(Kkozlowski89)
November 13, 2010, 4:54pm
5
jacmoe
(Jacob Moena)
November 13, 2010, 5:14pm
6
Yes:
<?php echo $form->dropDownList($model, 'issue_priority_id', CHtml::listData(
IssuePriority::model()->findAll(array('order'=>'id')), 'id', 'name'), array('options' => array('2'=>array('selected'=>true)))); ?>
jacmoe
(Jacob Moena)
November 13, 2010, 5:16pm
7
Cool! I used the same topic when I wanted to figure out how to do that.
jzu
(Tiger Jackson)
November 13, 2010, 5:47pm
8
But any is there any different comparing with setting default values to the model instead??
charles07
(Charles Eapen)
February 23, 2012, 6:55am
9
jacmoe, u r a life saver. Thank you very much.
jacmoe:
You don’t have to do that.
I think this would work:
<?php echo CHtml::dropDownList('name', '0', array('0'=>'value1', '1'=>'value2'),
array('options' => array('1'=>array('selected'=>true)))); ?>