Difference between #1 and #2 of
Creating a dependent dropdown

Revision #2 has been created by wei on Mar 24, 2009, 10:32:36 PM with the memo:

Fixed wrapping code blocks.
« previous (#1) next (#3) »

Changes

Title unchanged

Creating a dependent dropdown

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Content changed

[...]
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.
[...]
83 2
92 followers
Viewed: 416 861 times
Version: 1.1
Category: Tutorials
Written by: dalip
Last updated by: Paul_Kish
Created on: Mar 23, 2009
Last updated: 9 years ago
Update Article

Revisions

View all history