Ajax Update Of View Including Dependent Dropdown

I have a view that is to be updated by a dropdownlist selection. This view also includes another dependent dropdown (id_types). Is there a straight forward way of doing this? I can use a renderPartial for the view update, but it does not update the dropdown selections?? If I use the dependent dropdown controller setting html options directly, I cannot update the view??

Thanks





Selection Dropdown:


<?php

     echo CHtml::dropDownList('id_category', $assetData['category'], $assetData['categories'], array('prompt'=>'---Select---',

                                                             'ajax'=>array(

                                                             'type'=>'POST',

                                                             'url'=>yii::app()->createUrl('asset/AssetTypeData2'),

                                                             'data'=>array('category'=>'js:jQuery(this).val()'),

                                                             'update'=>'#type_list',

                                                             ),

                                                              

                                                             ));   

?>

Controller Action:


  public function actionAssetTypeData2(){

        $key = $_POST['category'];

        $selected = '';

        

        if(isset($_POST['selected'])){

            $selected = $_POST['selected'];

        }

        

        $model = new AssetsForm();

        $assetData['types'] = $model->getAssetTypes($key);

        $assetData['type'] = $selected;

        $this->renderPartial('/asset/_type', array('assetData'=>$assetData));

    }


View to update:


<span id='type_list'>

<?php

     echo CHtml::dropDownList('id_types', $assetData['type'], $assetData['types'], array('prompt'=>'---Select---',

                                                            'ajax'=>array(

                                                            'type'=>'POST',

                                                            'url'=>yii::app()->createUrl('asset/AssetAttributeData'),

                                                            'data'=>array('type'=>'js:jQuery(this).val()','timestamp'=>'js:jQuery("#h2_timestamp").val()'),

                                                            'update'=>'#attribute_list',

                                                            ))); 

                                                            

    echo CHtml::button('Add Type', array('onclick'=>"checkCategory('#dialogAddType');", 

                                    'id'=>'add_type','class'=>'assetControl1'));   

    echo CHtml::button('Delete Type', array( 'submit'=>array('asset/DeleteType'), 'class'=>'assetControl2', 

                                    'id'=>'del_type','confirm'=>'Are you sure?'));

    echo CHtml::button('Edit Type', array('onclick'=>"checkCategory('#dialogModifyType')",

                                    'id'=>'mod_type','class'=>'assetControl3'));

    echo '<br/><br/></br>';

          

    $this->renderPartial('/asset/_attribute', array('assetData'=>$assetData));               

?>

</span>



I found a solution using JSON format allowing multiple updates in case anyone runs into this situation:





Selection:


 CHtml::dropDownList('id_category', $assetData['category'], $assetData['categories'], array('prompt'=>'---Select---',

                                                             'ajax'=>array(

                                                             'type'=>'post',

                                                             'url'=>yii::app()->createUrl('asset/AssetTypeData2'),

                                                             'datatype'=>'json',

                                                             'data'=>array('category'=>'js:jQuery(this).val()'),

                                                             'success'=>'function(result){ 

                                                                var response = $.parseJSON(result);

                                                                $("#id_types").html(response.options);

                                                                $("#attribute_list").html(response.updates); }'

                                                             ),

                                                              

                                                             ));   

?>


Controller Action:


   public function actionAssetTypeData2(){

        $key = $_POST['category'];

        $selected = '';

        

        if(isset($_POST['selected'])){

            $selected = $_POST['selected'];

        }

        

        $model = new AssetsForm();

        $assetData['types'] = $model->getAssetTypes($key);

        $assetData['type'] = $selected;

        $assetData['gridData'] = array();

        $result = array('options' => $this->actionAssetTypeData());

        $result['updates'] = $this->renderPartial('/asset/_attribute', array('assetData'=>$assetData),true);

        echo CJSON::encode($result);

    }


Updated Views:


<?php

    echo CHtml::dropDownList('id_types', $assetData['type'], $assetData['types'], array('prompt'=>'---Select---',

                                                            'ajax'=>array(

                                                            'type'=>'POST',

                                                            'url'=>yii::app()->createUrl('asset/AssetAttributeData'),

                                                            'data'=>array('type'=>'js:jQuery(this).val()','timestamp'=>'js:jQuery("#h2_timestamp").val()'),

                                                            'update'=>'#attribute_list',

                                                            ))); 

?>

</span>


<span id='attribute_list'>

<?php

    $attributeFormat = 'Text';

    $attributeDefault = '';

    

...

</span