remember filters for CGridView

For the afterConstrruct()… once again… good idea :D

You can change the call from $this->owner->saveSearchValues()

to $this->saveSearchValues()

same for read…

and declare save/read as private as they are not needed outside this class…

I am posting the full code, if you notice something tell me:




/**

 * Helper to remember search filters on site

 */

class SearchRememberManager extends CActiveRecordBehavior {


    /**

 	* This will read the values from the session and write to the properties

 	*/

    private function readSearchValues() {

        $modelName = get_class($this->owner);

        $attributes = $this->owner->getSafeAttributeNames();


        foreach ($attributes as $attribute) {

            if (null != ($value = Yii::app()->user->getState($modelName . $attribute, null))) {

                $this->owner->$attribute = $value;

                //Yii::app()->user->setState($modelName . $attribute, 1, 1);

            }

        }

    }


    /**

 	*

 	*/

    private function saveSearchValues() {


        $modelName = get_class($this->owner);

        $attributes = $this->owner->getSafeAttributeNames();

        foreach ($attributes as $attribute) {

            // echo $this->owner->$attribute."<br>";

            if (isset($this->owner->$attribute)) {

                Yii::app()->user->setState($modelName . $attribute, $this->owner->$attribute);

            }

        }

    }


    /**

 	*

 	* @param type $event

 	*/

    public function afterConstruct($event) {

        if ($this->owner->scenario == 'search') {

            $this->owner->unsetAttributes();


            if (isset($_GET[get_class($this->owner)])) {

                $this->owner->attributes = $_GET[get_class($this->owner)];

                $this->saveSearchValues();

            } else {

                $this->readSearchValues();

            }

        }

    }


}

All OK… you can post this as an extension…

I did it. Here it is: http://www.yiiframework.com/extension/remember-filters-gridview/

Nice addition to Yii…

Just one thinking… as you moved the default code (generated by Gii) to the behavior… you could write an "actionAdmin" example to show how to use this behavior…

Because with this extension the actionAdmin instead of the classic




	public function actionAdmin()

	{

		$model=new Company('search');

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

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

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

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

			'model'=>$model,

		));

	}



can be as simple as:




	public function actionAdmin()

	{

		$model=new Company('search');

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

			'model'=>$model,

		));

	}



Thanks, I’ve updated the documentation.

Just a note… in the doc you write actionXXX() but in the example you let actionAdmin() - it could posibly confuse… better to use the same name in the doc and example… and posibly you can adjust the spaces…

Nice work, pentium10.

Would be nice to save page and sorting information too, in order to retourn to the preious page.

I guess that for this we have to change sort and pagination in the data provider.

What do you think about it?

Thank you, that is a very great recommendation. I will think it through.

Thank you for sharing your work

I also needed my CGridView to remember the filtration when navigating between pages and i could not find any simple examples of how to use session, so i used the suggestion of Session and modified a standard controller this way:

Standard "Crud" Action:




public function actionAdmin() {

		$model = new Post('search');

		$model->unsetAttributes();


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

			$model->setAttributes($_GET['Post']);


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

			'model' => $model,

		));

	}




Modified Action:




public function actionAdmin() {

		$model = new Post('search');

		$model->unsetAttributes();




		if (isset($_GET['Post']))  // user makes change in filter, store in session

		{

			$model->setAttributes($_GET['Post']);

			Yii::app()->session['Post'] = $_GET['Post'];

			

		} else {   // the user comes back from another page, session sets filter

				if (isset(Yii::app()->session['Post']))

				{

					$model->setAttributes(Yii::app()->session['Post']);

				}

		}	


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

			'model' => $model,

		));

	}




Its maybe not the Yii way to do it, but its on way… :blink: