Creating a dependent dropdown

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

Often you'll need a form with two dropdowns, and one dropdown's values will be dependent on the value of the other dropdown. Using Yii's built-in ajax functionality you can create such a dropdown. Below will be demonstrated how you can achieve this.

First the view with the form. We'll show a form that shows countries and dependent of the country selected will show cities.

echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
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

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.

Next is the controller action, it will have to output the html to fill the second dropdownlist. Furthermore it will do that dependent on the the value of the first dropdown.

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 tag and the output will end up in the second dropdown.

You might wonder where the $_POST['country_id'] comes from. It's simple, when the 'data' key of the ajax array in the first dropdown is empty, all values of the elements of the form the dropdown is in, will be passed to the controller via the ajax request. If you're using Firebug you can inspect this request and see for yourself.

This behaviour can also be changed. By default the value of the 'data' key in the ajax configuration array is js:jQuery(this).parents("form").serialize(). The preceding js: indicates to Yii that a javascript statement will follow and thus should not be escaped. So, if you change the 'data' key to something else preceded by 'js:' you can fill in your own statement. The same applies to the 'success' parameter.

83 2
92 followers
Viewed: 416 996 times
Version: Unknown (update)
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

Related Articles