Revision #2 was created by wei on Mar 24, 2009, 10:32:36 PM.
Fixed wrapping code blocks.
Content
[...]
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>'dynamiccities', //url to call
'update'=>'#city_id', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
)));
echo CHtml::dropDownList('city_id','', array()); //empty since it will be filled by the other dropdown
echo CHtml::dropDownList('city_id','', array());
```
The first dropdown is filled with several value/name pairs of countries. Whenever it is changed an ajax request will be done to the 'dynamiccities' action of the current controller. The result of that request (output of the 'dynamiccities' action) will be placed in the second dropdown with id is #city_id.
[...]
public function actionDynamiccities()
{
$data=Location::model()->findAll('parent_id=:parent_id',
array(':parent_id'=>(int) $_POST['country_id']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
```
It will retrieve all cities that have as a parent the id of the first dropdown. It will then output all those cities using the <option> tag and the output will end up in the second dropdown.