Dependent Dropdown List On Update

Hi,

The dependent dropdown code works on create page very nicely. When I select / change the parent dropdown list option, the child options gets populated. But I am not able to get it working on update page.

I have a div and the update form gets loaded into this via ajax. The parent dropdown list have the country selected already as it is update form, but the child dropdown list is empty because we not changing the option of parent dropdown list.

Please tell me how to do this.

Awaiting for the response.

Thanks already.

you can solve this by setting dropdown’s options when record is not new

Example:




echo $form->dropDownList($model,'country', Country::model()->findAll(),

                            array(

                                'prompt'=>' ',

                                'ajax' => array(

                                'type'=>'POST',                          

                                'url'=>CController::createUrl('listCities'),

                                'update'=>'#'.CHtml::activeId($model,'city')

                                 )));


echo $form->dropDownList($model,'city',(($model->isNewRecord) ?  

                    array() : City::model()->findAllByAttributes(array(country=>$model->country))));



Hi Reza m,

Thanks for the reply.

Instead of findByAttributes, findAll worked for me.




City::model()->findAll('country_id=:country_id', array(':country_id' => (int)$model->country_id)



Thanks!