Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown list values not updating properly in Yii

I have three dropdown list that are dependent on each other. Here is the code:

 echo CHtml::dropDownList('island_id',$island,$locationsList, 
                            array('ajax'=>array('type'=>'POST',
                                                'url'=>CController::createUrl('supplierHead/getRegions'),
                                                'update'=>'#region_id',),
                                  'id'=>'island_id',
                                  'empty'=>'Choose Island',
                            )); 
    echo " ";
    echo CHtml::dropDownList('region_id',$region, $regionsList, 
                            array('ajax'=>array('type'=>'POST',
                                                'url'=>CController::createUrl('supplierHead/getProvinces'),
                                                'update'=>'#province_id',),
                                  'id'=>'region_id',
                                  'empty'=>'Choose Region',
                            ));
    echo " ";
    echo CHtml::dropDownList('province_id',$province,$provincesList, array('empty'=>'Choose Province'));

island_id is the parent and when a value is selected, it updates the 2nd dropdown and populates it with data relative to the parent, then once the value for the 2nd dropdown is selected, it populates the available values for the 3rd dropdown.

Problem:

When all drop downs are set with values and I decide to change the value of the first dropdown (parent), it updates the values of the 2nd dropdown, but not the 3rd. The 3rd dropdown is only updated when i manually make a change of a value from the 2nd dropdown. Also, when the 2nd dropdown value is changed, the 3rd dropdown updates which is correct, but the parent does not change. How can I fix this?

Here's my controller:

public function actionGetRegions()
{
    $data=Locations::model()->findAll('location_parent=:location_parent AND location_id != :location_parent', array(':location_parent'=>(int) $_POST['island_id']));
    $data=CHtml::listData($data, 'location_id', 'location_name');

    foreach ($data as $value => $name) {
        echo CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true);
    }
}

public function actionGetProvinces()
{
    $data=Locations::model()->findAll('location_parent=:location_parent AND location_id != :location_parent', array(':location_parent'=>(int) $_POST['region_id']));
    $data=CHtml::listData($data, 'location_id', 'location_name');

    foreach ($data as $value => $name) {
        echo CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true);
    }
}
like image 704
remedy. Avatar asked Nov 10 '22 12:11

remedy.


1 Answers

 echo CHtml::dropDownList('island_id',$island,$locationsList, 
                            array('ajax'=>array('type'=>'POST',
                                                'url'=>CController::createUrl('supplierHead/getRegions'),
                                                'update'=>'#region_id',),
                                  'id'=>'island_id',
                                  'empty'=>'Choose Island',
                            )); 
    echo " ";
    echo CHtml::dropDownList('region_id',$region, $regionsList, 
                            array('ajax'=>array('type'=>'POST',
                                                'url'=>CController::createUrl('supplierHead/getProvinces'),
                                                'update'=>'#province_id',),
                                  'id'=>'region_id',
                                  'empty'=>'Choose Region',
                            ));
    echo " ";
    echo CHtml::dropDownList('province_id',$province,$provincesList, array('empty'=>'Choose Province'));

That's all ok you must show your controller file

or else use this

Its on your controller

    $data=CHtml::listData($data,'id','name'); //$data depend with the POST id 
    foreach($data as $value=>$name)
    {
        echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
    }
like image 77
Babu Avatar answered Nov 14 '22 22:11

Babu