Pass Filtered Data From Cgridview To Another View

I have a CGridView which displays data (from a model). Now when I click on some row, I open a new view with details of this item.

What I want to do is, that after filtering the data and clicking the row (one of the filtered), new view will appear that will know

  1. id of the clicked row

  2. ids of the filtered rows in previous view (and not only rows displayed on the current page, but all rows that were filtered)

The first one is easy, but I don’t know how to get the filtered items. How could it be done?

You can do it two ways, when opening the second view, when passing id of clicked row, also pass:

  1. all other ids

  2. whole filter form used to fetch data and then fetch only the ids

For the first point, remember that CGridView holds ids of all loaded rows in a hidden DOM container.

The second solution may seem an overkill at first glance, but it could allow to slightly alter the filter.

I once used this in a scenario when I had to display a ‘previous/next’ links in the details view. A range of ids would be fine, except for the first and last element on the list, that requires the last id from previous page or first from the next one.

Now if you have all the filter used to fetch data you can just modify the current page and fetch the proper ids.

After adding caching this didn’t do to many heavy queries and was quite flexible.

Thank you for your reply, the second solution looks better, as is it’s possible that I’ll make some modification then. Could you please give me an example how to pass the filter? This is how the form and CGridView looks like now (without the javascript):




<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>

<div class="search-form" style="display:none">

<?php $this->renderPartial('_search',array(

	'model'=>$model,

)); ?>

</div>

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

	'id'=>'photo-grid',

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

 	'filter'=>$model,

        'selectableRows'=>1,

        'selectionChanged'=>'function(id){ location.href = "'.$this->createUrl('showPhoto').'/id/"+$.fn.yiiGridView.getSelection(id);}',

	'columns'=>array(

        

                'name',

		'date',

		array(

			'class'=>'CButtonColumn',

            'template'=>'{view}',

            'header'=>CHtml::dropDownList('pageSize',$pageSize,array(20=>20,50=>50,100=>100),array(

                'onchange'=>"$.fn.yiiGridView.update('photo-grid',{ data:{pageSize: $(this).val() }})",

            )),

		),

	),

));



I can’t give you an example because I use my own custom grid class and filter form.

Try to work out how CGridView passes your search form when making a request to load data. Probably it just serializes a form.