Clear $model attribute on dropdownlist change

Let me try to explain what I am doing:

I have an independent dropdownlist that when you select an item, it populates another dropdownlist that it is part of the search of a model. It populates with an ajax call to an action on my controller. Now when I select from the second dropdown it submits the form and cgridview populates and is working fine. The question/problem that I have is that when you select again from the first dropdownlist the cgridview keeps the info and doesnt refresh, any ideas on how can I unset model attributes from a ajax post call?

could you please provide the code? I think it would be helpful for ppl trying to help

:)

tks

Ok, here is my code:

this is the first dropdownlist




<div class="row" style="margin:auto">

		<?php echo 'Seleccione Seccion'; ?>

		<?php echo CHtml::dropDownList('Secciones',$_GET['Secciones'],CHtml::listData(Secciones::model()->findAll(),'secciones','secciones'),array(

		'prompt'=>'Seleccione Seccion',

		'ajax' => array(

			'type'=>'POST',

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

			'update'=>'#Asignaciona_codigoal',

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

		)));?>

</div>



this is the dropdownlist that gets populated when i change the first one:




		<?php echo $form->label($model,'codigoal'); ?>

		<?php echo $form->dropDownList($model,'codigoal',CHtml::listData(Alumno::model()->findAllbySql('SELECT Codigo, concat( Nombres, " ", Apellidos ) AS Nombres FROM tbalumnos order by Apellidos'),'Codigo','Nombres'),array('prompt'=>'Seleccione Alumno','class'=>'dDList'));?>



When I select something from this second dropdowlist it triggers this javascript:




Yii::app()->clientScript->registerScript('search', "

	$('.dDList').change(function(){ 

	this.form.submit();

	return false;

});

$('.search-form form').submit(function(){

	$.fn.yiiGridView.update('asignaciona-grid', {

		data: $(this).serialize()

	});

	return true;

});	

");



This is the code from my controller:




public function actionSeccionesD()

	{


		$nombreS=$_POST['Secciones'];

		$sqlQ='SELECT Codigo, concat( Nombres, " ", Apellidos ) AS Nombres FROM tbalumnos where seccion ="' . $nombreS . '" order by Apellidos';

		$contar = Alumno::model()->findAllBySql($sqlQ);

		$data=CHtml::listData($contar,'Codigo','Nombres');

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

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

		{

		    echo CHtml::tag('option',

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

		}

	}



And this is the grid that I want to refresh when I select from the first dropdownlist:




$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'asignaciona-grid',

	'dataProvider'=>$model->search(),

	'summaryText'=>'',

	'columns'=>array(

		'codigom0.nombre',

		array

	(

		'class'=>'CButtonColumn',

		'template'=>'{delete}',

		'deleteConfirmation'=>'Borrar la asignacion de la materia?'

	)),

	'enablePagination'=>false,

));



If I use ‘success’ on my ajax to refresh the grid, it doesnt work because I still have a $_GET variable that points to my model and the grid uses that to search, so what I want to do is unset that variable, but I think I cant do that because then I would be using both a post and a get request on the same page, any ideas? I think the only way would be to hide the grid completely.

http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown

This wiki and the latest comment on it will be helpful.

Try using ‘success’ instead of ‘update’ for combo box ajax. You can call “$(’.search-form form’).submit()” from ‘success’ function to refresh the grid.

Thx Softark, I did that and works the same… the problem is that when I submit the form the $model still has the variable of the former list so when I send it, the variable for the grid is still the same.

Ok, I have narrow it down to this, How do I change a variable that I send in post?




'data'=>array('Secciones'=>'js:this.value','Asignaciona["codigoal"]'=>'')



If I get that to work everything will be solved ::)

Do you call the same controller action for (1) updating the dropdown contents, and for (2) updating the gridview? Why don’t you call 2 different actions?

The working sequence may be …

[list=1]

[*]User changes 1st dropdown

[*]The 1st dropdown will call the 1st action via ajax

[*]The 1st action (that receives $_POST) will send back the contents of the 2nd dropdown

[*]Updates the contents of the 2nd dropdown in ajax success function

[*]And then submit the search form in ajax success function too

[*]The search form will submit the refreshed search key of the 2nd dropdown

[*]The 2nd action (that receives $_GET) will send back the refreshed contents of the grid

[*]The grid will be updated

[/list]

In general, the 2nd action is just what gii has generated for "admin" action. "admin" action and the grid view and "advanced search form" should work fine out of the box. What you have to do for yourself is just the 1st half of the sequence.

i have one dropdownlist that selected value how i pass to controller …plz help me i’m new to yii…

i tried the below code but i can’t get the value …plz give a clear example…

model = new TCategoryDescription();

$model->parent_id ==‘0’;

echo $form->labelEx($model,‘MainCategoryName’);

echo $form->dropDownList($model, ‘cat_name’, CHtml::listData(TCategoryDescription::model()->findAll(array(‘condition’=>‘parent_id=0’)), ‘fk_i_cat_id’, ‘cat_name’),

array(‘empty’=>‘Select Main Category’),array(‘selected’=>‘true’,‘ajax’ => array(‘type’=>‘GET’,‘url’=>yii::app()->baseUrl.’/tcategorydescription/subtest’,

        )));

echo $form->error($model,‘cat_name’);