DonRico
DonRico

Reputation: 182

Yii dependent dropDownMenu with variable update target

I'm using standard Yii dependent dropDownMenu (something similar to this). The logic is that current dropDown will define which element (next dropDown) will be updated with controller action.

echo CHtml::dropDownList('level_1', '', $arrayWithValues, aaray(
  'ajax'=>array(
    'type'=>'POST',
    'url'=>Ccontroller::createUrl('myActionName'),
    'update'=>'#level_2'
)));

My question is how can I change the update value after the action is executed? i.e the action find out that we need to populate DropDown Level_3 instead of Level_2.

Upvotes: 0

Views: 460

Answers (1)

ineersa
ineersa

Reputation: 3445

echo CHtml::dropDownList('level_1', '', $arrayWithValues, aaray(
  'ajax'=>array(
    'type'=>'POST',
    'dataType' => 'json',
    'data'=>array('selected'=>'js:this.value'),,
    'url'=>Ccontroller::createUrl('myActionName'),
    'success'=>'js:function(data){
       if (data.update==2){
          $("#level_2").empty(); 
          $("#level_2").append(data.items);
       }
       if (data.update==3){
          $("#level_3").empty(); 
          $("#level_3").append(data.items);
       }
    }'
)));

In your controller you'll have selected value as $_POST['selected'].

public function actionmyaActionName()
{
    ....
    do what you need here
    $items.=CHtml::tag('option', array('value'=>$value),CHtml::encode($state),true);

    ....
    echo CJSON::encode(array(
    'update'=>$update,
    'items'=>$items
    ));
    Yii::app()->end();
}

Form your options, define what to update.

Upvotes: 1

Related Questions