I have the following active drop down list
<?php
echo CHtml::activeDropDownList($project, 'city', CHtml::listData(City::model()->findAll(), 'id', 'name'), array('class'=>'st-form', 'onchange' => 'getLocationByCity(this)'));
?>
I want to add selected option to the 10th value in the drop down list when the list is being created, how do I do it.
Thanks.
abennouna
(Abennouna)
April 15, 2012, 10:33pm
2
The thing is CHtml::activeDropDownList helper will automatically, by design, select the value (in your code, ‘id’) in the list corresponding to the value of $project->city (which I suppose matches ‘id’, not ‘name’). Meaning it will output the needed HTML to achieve that.
But If you use CHtml:dropDownList, you can specify the value to be selected.
Or if activeDropDownList is needed, you could simply have some jQuery code to change that selected value to the 10th one in the dropdown.
undefinable
(Obfuscated Geek)
April 15, 2012, 10:42pm
3
Hi bennouna,
I know it can be achieved by using javascript or a normal dropdownlist, what I want to know how to do it using activeDropDownList().
I didnt quite get your first part of the answer, can you please elaborate or give some code example if possible.
Thanks.
The thing is CHtml::activeDropDownList helper will automatically, by design, select the value (in your code, ‘id’) in the list corresponding to the value of $project->city (which I suppose matches ‘id’, not ‘name’). Meaning it will output the needed HTML to achieve that.
But If you use CHtml:dropDownList, you can specify the value to be selected.
Or if activeDropDownList is needed, you could simply have some jQuery code to change that selected value to the 10th one in the dropdown.
abennouna
(Abennouna)
April 15, 2012, 10:45pm
4
Hello. I was saying that you can’t do it otherwise (as far as I know) and I was giving 2 alternatives, which you knew already. Sorry.
undefinable
(Obfuscated Geek)
April 15, 2012, 10:46pm
5
thank you for your answers.
fkbr1993
(Fkbr1993)
February 16, 2014, 10:39pm
6
Hello!
I’m thinking, that I’ve found a solution.
You’re using active DropDownList and it means that:
You have a (blank) model, which was created before.
You have a value to be set as selected in php code.
So you can just make it by simple assigment:
$city_id = 10;
...
$project = new Project();
$project->city = $city;
...
echo CHtml::activeDropDownList($project, 'city', CHtml::listData(City::model()->findAll(), 'id', 'name'),);
Good luck!