Set CGridView filter parameters to a session variable

I am working with a CGridView. I am wanted to take the filter value (The actual value under the table header that is being filter and/or if the column is asc/desc) and set its value/values to a session variable, so that when leaving the page and coming back I can use the filter values again. From my exploration of the forum this is what I have gathered:

To get the CGridView filter parameters I need to use one of the follow:

Yii::app()->request->getParam(‘name’);

CHttpRequest::getParam(‘name’);

$_GET[‘name’];

To set a session:

Yii::app()->session[‘filterName’] = $x

Now where do I get the CGridView filter parameters? in the controller, view, model? And how do I set the CGridView filter parameters to my session variable?

Thanks for any and all help!

Filters work at data provider level (in controller).

So you should apply that filter in data provider query, such as:

in the controller




public function actionGrid()

{

     $filterX = Yii::app()->session['filterName'];


     $modelSearch = new ModelName();

     $modelSearch->filterName = $filterX;


     $cr = new CDbCriteria();

     $cr->compare('filterName', $filterX);


     $dataProvider=new CActiveDataProvider($modelSearch, array('criteria' => $cr));


     return $this->render('grid', array('modelSearch' => $modelSearch, 'dataProvider' => $dataProvider));

}







in the grid view




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

    'dataProvider' => $dataProvider,

    'filter' => $modelSearch,

    'columns' => array(

        'name',

        'address',

        ...

    ),

));




Thank you for your response! I am wondering how the session gets set after the first filter. So far this has not worked for me:

In the controller:




$x = Yii::app()->request->getParam('name');


if(isset($x)){

    Yii::app()->session['filterName'] = Yii::app()->request->getParam('name');

}



You can try:




public function actionGrid()

{

     $filterX = Yii::app()->getParam('filterName');

     if($filterX !== null)

     {

          Yii::app()->session['filterName'] = $filterX;

     }

     else

     {

          $filterX = Yii::app()->session['filterName'];

     }


     $modelSearch = new ModelName();

     $modelSearch->filterName = $filterX;


     $cr = new CDbCriteria();

     $cr->compare('filterName', $filterX);


     $dataProvider=new CActiveDataProvider($modelSearch, array('criteria' => $cr));


     return $this->render('grid', array('modelSearch' => $modelSearch, 'dataProvider' => $dataProvider));

}




Thank you once again for your reply!

I have added your code and now the filter for that column does not work. Also if I add the following code and enter a value in the filter hit enter nothing gets printed. If I leave the page and come back nothing gets printed:




     $filterX = Yii::app()->getParam('name');

     if($filterX !== null)

     {

          Yii::app()->session['filterName'] = $filterX;

          print($filterX);

     }

     else

     {

          $filterX = Yii::app()->session['filterName'];

          print($filterX);

     }



I don’t think Yii::app()->getParam(‘name’) is receiving the parameter. The actual name of the model I am using is called “Dealers”. In that model there is a field called “name” that I am need to capture the filter/sorting of it’s CGridView in a certain view. I hope that better clarifies my question.

Thanks once again for all your help.

If model name is ‘Dealer’, parameter should be ‘Dealer[name]’, so:




     $filterX = isset($_GET['Dealer']['name'])?$_GET['Dealer']['name']:null;


     if($filterX !== null)

     {

          Yii::app()->session['filterName'] = $filterX;

          print($filterX);

     }

     else

     {

          $filterX = Yii::app()->session['filterName'];

          print($filterX);

     }



Try and let me know if works.

Sorry I did not tell you the correct model name it is ExtendedDealers… because it extends the basic dealer model.

Even with changing Dealers to ExtendedDealers, and adding a print_r($_GET); in the controller it will only print Array()… here is my code:

Model ExtendedDealers.php:




class ExtendedDealers extends Dealers {


    ...


    public function search()

    {


        $criteria=new CDbCriteria;


        $criteria->compare('did',$this->did);

        $criteria->compare('dgid',$this->dgid);

        $criteria->compare('lid',$this->lid);

        $criteria->compare('name',$this->name,true);

        $criteria->compare('address1',$this->address1,true);

        $criteria->compare('address2',$this->address2,true);

        $criteria->compare('city',$this->city,true);

        $criteria->compare('state',$this->state,true);

        $criteria->compare('zip',$this->zip,true);

        $criteria->compare('country',$this->country,true);

        $criteria->compare('phone',$this->phone,true);

        $criteria->compare('web',$this->web,true);

        $criteria->compare('timezone',$this->timezone,true);

        $criteria->compare('status',$this->status,true);


        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

            'sort'=>array(

                'defaultOrder'=> 'name ASC',

            )));

    }

}



Controller DealerController.php:




class DealersController extends Controller

{

    ...


	public function actionAdmin()

	{

		print_r($_GET);


		$filterX = isset($_GET['ExtendedDealers']['name'])?$_GET['ExtendedDealers']['name']:null;

		if(isset($filterX)){

			print($filterX);

		}

		$model=new ExtendedDealers('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['ExtendedDealers']))

			$model->attributes=$_GET['ExtendedDealers'];


		$this->render('admin',array(

			'model'=>$model,

		));

	}

}



View admin.php




...


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

	'id'=>'dealers-grid',

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

	'filter'=>$model,

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

    'columns'=>array(

		'name',

		'address1',

		'city',

		'state',

		'phone',

                ...


	),

)); 

?>



Let me know if there is anything you see that is keeping $_GET[‘ExtendedDealers’][‘name’] or even $_GET from printing.

Thanks!

if




var_dump($_GET)



returns an empty array, the problem is how this action is called.

Are you sure that parameters have been passed?

var_dump gave me:

array (size=0)

empty

Are you talking about “filter parameters” being passed? If so I don’t know.

I’m talking about when this action will be called.

When paramters will be sent to this action?

action?.. Are you talking about:

actionAdmin in the controller,

how and when are actionAdmin is called?

something in the CGridView?

‘params’ => array(…) has not been set