Cgriview Ajax Render Partial With Sort

I have the following code on an index page which creates a dropdown with an ajax function


echo CHtml::dropDownList('','filter', CHtml::listData(Models::model()->findAll(array()), 'id', 'model'),array('empty' => Yii::t('app','misc.pleaseselect'),"onChange"=> CHtml::ajax(array("type"=>"POST","dataType"=>"json",

								    																	"url"=>array("Administration/showFiltersGrids"),

								    																	"data"=>array("model_id"=>"js: this.value"),

								    																	"success"=>"function(data){

								    																

								    																		$(\"#filtersarea\").html(data.gridViews);


								    																		}",

								    																	)


								    																)

																									));

																								 ?>	

Upon selection this uses an action to generate a data provider and then render partial a page that contains a CGridview, passing the data provider with it.





	/*

	** This is an ajax rendering of the filter field grid dependent on the posted data

	*/

	public function actionshowFiltersGrids()

	{

		$dataProvider = Filters::model()->dpAdministration($_POST['model_id']);

	

	 	$gridViews[] = $this->renderPartial("_grid_filters", array('dataProvider' => $dataProvider), true);

	    echo CJSON::encode(array('gridViews'=>$gridViews));

	} 



This works perfectly fine and renders the gridview on every change of the dropdown. The issue I am having however, is that both sort and pagination result with Yii trying to load the page in which the grid is held. Resulting in a display like so

What I need Ideally is to make the sort function an ajax update, can someone point me in the right direction of how to solve this.

try this pass the ProcessOutput when page render

like


this->renderPartial('index',array('model'=>$model),false,true);

and if this tric not working simplay jqyery.js false when action was called




$cs = Yii::app()->clientScript;

        $cs->reset();

        $cs->scriptMap = array(

            'jquery.js'  =>  false,   // prevent produce jquery.js in additional javascript data

        );

Finally managed to get this resolved. The first thing is that the sort is to do with the data provider and not with the cgridview. In order to resolve the issue I had to add both route and params under the sort array.

View Code




echo CHtml::dropDownList('','filter', CHtml::listData(Models::model()->findAll(array()), 'id', 'model'),array('empty' => Yii::t('app','misc.pleaseselect'),"onChange"=> CHtml::ajax(array("type"=>"POST","dataType"=>"json",

									    																	"url"=>array("Administration/showFiltersGrids"),

									    																	"data"=>array("model_id"=>"js: this.value","first"=>true),

									    																	"success"=>"function(data){

									    																

									    																		$(\"#filtersarea\").html(data.gridViews);


									    																		}",

									    																	)


									    																)

																										));

																									 ?>		



This creates the ajax action to the controller passing first as true , to distinguish between the first request or a sort request.

Controller Code




/*

	** This is an ajax rendering of the filter field grid dependent on the posted data

	*/

	public function actionshowFiltersGrids()

	{

		Custom::addLog($_REQUEST);

		$dataProvider = Filters::model()->dpAdministration($_REQUEST['model_id']);

		if($_REQUEST['first'])

		{

			$gridViews[] = $this->renderPartial("_grid_filters", array('dataProvider' => $dataProvider), true,true);

			echo CJSON::encode(array('gridViews'=>$gridViews));

		}

		else

		{

			

			echo $this->renderPartial("_grid_filters", array('dataProvider' => $dataProvider), true);

		}

	} 



Model Code - For Data Provider. Here we see the router and params in action, this allows me to determine the correct data in the controller method (again first is not specified here so we know it is a sort)




	/*

	** This method returns the filters both visible and not visible depending on the data passed over 

	*/

	public function dpAdministration($model_id)

	{

		$dataProvider=new CActiveDataProvider('Filters', array(

				    'criteria'=>array(

				    'condition'=>'model_id = :model_id',

				    'params'=>array(':model_id'=>$model_id),

				    'group'=>'model_id, name',

			    ),	

			    'pagination'=>array(

			        'pageSize'=>10,

			    ),

			   'sort'=>array(

        				'route'=>'Administration/showFiltersGrids',

        				'params'=>array('model_id'=> $model_id),

        				),

		));

		return $dataProvider;

	}



_grid_filters Code which is the view that is partially rendered




<div class="small-12 columns">

	<fieldset>

		<legend><?php echo Yii::t('app','misc.filters'); ?></legend>

		<?php

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

							'id'=>'filters-grid',

							'ajaxUpdate' => true,

							'template'=>'{items}{summary}{pager}',

							'cssFile'=> Yii::app()->request->baseUrl . '/css/foundation.css',

							'summaryCssClass'=>'right block clear',

							'pagerCssClass'=>'right block clear',

							'ajaxUrl' => $this->createUrl('index'),

							'dataProvider'=>$dataProvider,


							'columns'=>array(

								array(

										'name'=>Yii::t('app','model.filters.model_id'),

										'value'=>'$data->rl_model->model',

									),

								'name',




							    array(

					                'class' => 'CButtonColumn',

					                'template' => '{update}',

			                       'buttons'=>array(

							                		'update'=>array(


							                					'url'=>'CController::createUrl("/Filters/update/$data->id")',

							                					'imageUrl'=>Yii::app()->request->baseUrl.'/images/update.png',


							                			),

							                		)

								),


							)));




	?>

	</fieldset>

</div>




I have not yet done the pagination side of things. But I cannot see this being much different. However, the route and params needs to go under the pagination as well as under sort. Hopefully this will help someone but there is a full implementation of an ajax dropdown leading to a cgridview/data provider depending on the selection and fields are sortable.

Gotta love Yii, there is always something in the documentation to help you achieve the impossible.Is it the best, yii.