Dependent Drop Down

I am creating dependent drop down , I have one main drop down which I fill while loading the view the first drop down is as follows

==

<div class="row">

	&lt;?php 		


	echo CHtml::dropDownList('function_parent','',&#036;this-&gt;getjobtype(),


			array(


					'ajax' =&gt; array(


							'type'=&gt;'POST',


							'url'=&gt;CController::createUrl('cand/getcndchield'), 								


							'update'=&gt;'#function_id',					


					)));		


	 ?&gt;


	


&lt;/div&gt;

==On selecting one of the options I am populating the second dropdown as follows

==

<div id="row">

<?php echo CHtml::dropDownList(‘function_id’,’’, array());?>

</div>

==

It works fine , the only thing I am not able to figure out how to keep the second drop down hidden initially , and make it visible when the AJAX call falls back.

==

I got it solved using success callback instead of ‘update’ , I am associating a javascript function on success callback , and using jquery selectors to change the html of the dependent dropdown, I am not sure if there is a more appropriate way to do this. what I am doing right now is as follows

echo CHtml::dropDownList(‘function_parent’,’’,$this->getjobtype(),

‘ajax’ => array(

‘type’=>‘POST’,

							'url'=&gt;CController::createUrl('cand/getcndchield'), 								

‘success’=>'js:function(data){-----JS CODE----}

)));

Hi,

please share the code for ‘success’=>'js:function(data){-----JS CODE----}

And there’s another request from me if you are kind to share.

If AJAX call falls back it doesn’t keep the 2nd dropdown selected value.

And on update when it loads the form page it happens the same

Might find this useful. Check this link out on dependent dropdowns…

yii dependent dropdown

cheerz! :)

Well, this is exactly what i have on my code and is not working.

Here is solution that worked in my case:

_form.php




<?php $form->widget('zii.widgets.jui.CJuiDatePicker', 

		array(

			'model' => $model,

			'attribute' => 'date',

			'value' => $model->date,

			'options' => array(

					'showButtonPanel' => true,

					'changeYear' => true,

					'dateFormat' => 'yy-mm-dd',

			),

			'htmlOptions' => array(

					'placeholder'=>'yy-mm-dd',

					'ajax' => array(

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

						'url'=>CController::createUrl('fighters/DynamicFighters'),	//url to call.

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

						'data'=>array('date'=>'js:this.value', 'id_home'=>'js:$("#Matches_id_home").val()'),

					),

			),

		));

?>


...

more code

...


<?php Yii::app()->clientScript->registerScript(uniqid(), "

id_away = $('#Matches_id_away').val();


$(window).load(function(){

	$.ajax({

		type	: 'POST',

		url	: '" . $this->createUrl('fighters/DynamicFighters') . "',

		data	: { 'id_home': $('#Matches_id_home').val(), 'date': $('#Matches_date').val() },

		success : function (data) {

			$('#Matches_id_away').html(data);

			$('#Matches_id_away').val(id_away);

		},

	});

});

");

?> 



controller.php




public function actionDynamicFighters()

{

	$homecrt= new CDbCriteria();

	$homecrt->condition	= 'id_fighter = :id_fighter

				AND change_date <= :change_date';

	$homecrt->params	= array(':id_fighter'=>(int) $_POST['id_home'],

				':change_date'=>$_POST['date']);

	$homecrt->order		= 'change_date DESC';

	$home = FightersUpd::model()->find($homecrt);

	

	if(!$home)

	{

		// Ia liga curenta din datele fighterului

		$home = Fighters::model()->findByPk((int) $_POST['id_home']);

	}

		

	$away = Fighters::model()->with(array(

			'fightersUpds' => array(

				'alias'	=> 'f',

				'select'=> 'id',

				'condition' => 'f.change_date <= :change_date

						AND f.stop_date > :stop_date

						AND f.id_league = :id_league

						AND f.id_fighter != :id_fighter',

				'params' => array(

						':change_date' => $_POST['date'],

						':stop_date' => $_POST['date'],

						':id_league' => (int) $home->id_league,

						':id_fighter' => (int) $_POST['id_home']

				))))->findAll(array(

				'select'=> 'id,name,surname,id_league',

				'order'=> 'name',

	));

		

	$away = CHtml::listData($away,'id','FullName');

		

	echo '<option value="">= Select Away Fighter =</option>';

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

	{

		echo CHtml::tag('option',

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

	}

}



Hope it helps someone