Dependent DropDown

Hi,

I’m new to yii.

I have to create a suite of dependet dropdownlist, but i need your help.

This is my code in the _form.php




<div class="row">

		<?php echo $form->labelEx($model,'mut_immobile_prov_sigla'); ?>

                <?php 

                    echo $form->dropDownList($model,'mut_immobile_prov_sigla', CHtml::listData(TabProvince::model()->findAll(), 'prov_sigla','prov_descr'), array('empty'=>'',

                            'ajax' => array(

                            'type'=>'POST', //request type

                            'url'=>CController::createUrl('DataMutuiController/dynamiccities'), //url to call.

                            //Style: CController::createUrl('currentController/methodToCall')

                            'update'=>'#mut_immobile_com_id', //selector to update

                            //'data'=>'js:javascript statement' 

                            //leave out the data key to pass all form values through

                            )

                        

                        )); 

                ?>

		<?php echo $form->error($model,'mut_immobile_prov_sigla'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'mut_immobile_com_id'); ?>

                <?php echo $form->dropDownList($model,'mut_immobile_com_id', array(), array('empty'=>'')); ?>

		<?php echo $form->error($model,'mut_immobile_com_id'); ?>

	</div>



and this in the controller




public function actionDynamiccities()

        {

            $data=Location::model()->findAll('parent_id=:parent_id', 

                          array(':parent_id'=>(string) $_POST['mut_immobile_prov_sigla']));


            $data = CHtml::listData(TabComuni::model()->findAll(), 'com_id','com_descr');

            foreach($data as $value=>$name)

            {

                echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);

            }

        }



I have copied it from the tutorial but it won’t work.

Anyone can help me please?

Thank you :)

This should generate the correct dropDownList id




...

'update'=>'#'.CHtml::activeId($model, 'mut_immobile_com_id'), //selector to update

...



/Tommy

I try this but no effect. The page seems to be not refreshed.

The same for me.

I verified that the ajax call to the controller action is correctly made.

But the second dropdown is never updated.

Any other suggestion?

how about


'url'=>CController::createUrl('DataMutuiController/Dynamiccities'),

rather than


'url'=>CController::createUrl('DataMutuiController/dynamiccities'),

because your function is : function actionDynamiccities()

I got the solution for my case!

The problem was that the ‘update’=>’#some_field’, updates the html of that div with this javascript code: jquery("#some_field").html(data)

Instead I removed the ‘update’ key and added:

‘success’=>‘js:function(data){jquery(’#some_field’).val(data)}’.

I hope it will help everyone with this problem.

Going to bump this thread rather than starting a new one.

I have two dropdown lists, with the 2nd dependent on the first. I have managed to get it to work find onChange of the first list. My question is as to how it is best to trigger this to on page load? i.e. so that the dependent list is loaded based on the default value sitting in the first dropdown list?

On page load you can set the values of the first dropdown and filter the corresponding values in the second one.

See the usage of the cascadedropdown extension.

Maybe the extension is useful for you.

Thanks mate, I’ll give that a try.

I am going to continue on this post since there are some slight relationship…

I am working on a “dependent” input form, meaning if the user selects ‘Other’ from a pull-down list, I generate dynamic input field for them to enter whatever they like. However, for some reason I cannot read what they entered. There is do data in the $_POST variable. I am not sure of what I am doing wrong…need some help fast. - THANKS A BUNCH!!

My view…




<?php echo $form->dropDownList($model,'_have_pets', CHtml::listData(Questionoption::model()->findAll(

			 'questionid=:questionid',array(':questionid'=>551) 

			 ), 'answer', 'answer'), 

                         array('ajax'=>array(

		              'type'=>'POST', 

		              'data'=>array('_have_pets'=>'js:this.value'),  

                               'url'=>CController::createUrl('profile/othertextbox'),

                            'update'=>'#other_id')

		   ),

		 array('empty'=>'--please select--')

		 ); ?>

<?php echo $form->error($model,'_have_pets'); ?>


<div id="other_id"></div>




controller




 

    public function actionOtherTextBox()

    {

     

	 $model = new Profile;


	 if(trim(strtolower($_POST['_have_pets'])) == trim(strtolower("Other"))){

	     echo CHtml::textField('_other_pets','', array('size'=>32,'maxlength'=>25));

	     /**** DEBUG ****

		 echo "<pre>";

		  var_dump($_POST);

         echo "</pre>";

 	     ***/

	  }

	}

		



no value is in the $_POST variable for the dynamic field.




$_POST['Profile']['_other_pets']



I hope I am not missing something, but I am not sure I put my point across correctly first time.

Basically, I am looking at using this in model CRUD in a Yii application. I understand the concept of triggering dependent lists based on contoller methods. However, aside from this technical aspects, how do I approach the issue when I am using models as I need the correct value loaded when updating Active Records but obviously not during creating a new record as they values do not exist.

Which is the best approach to this scenario?