Store Advanced Search Items In Gridview

I’ve got a lot of fields in my table, and not all of them are visible in the gridview.

If I use the advance search I can search on the fields that are not in the gridview, but now I want to refine the search I ended up with in the gridview.

But as soon as I search again in the gridview, all of the advance search values are lost.

Is there a way I can store those values and use them to refine my gridview? The only time they have to reset is when I reload the page.

Hi Johannn,

By storing the search parameters in the user’s session, you may satisfy 90% of your needs.

Using CWebUser::setState() and CWebUser::getState() …




public function actionAdmin()

{

	$model = new MyModel('search');

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

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

	{

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

		Yii::app()->user->setState('MyModelSearch', $_GET['MyModel']);

	}

	else

	{

		$search = Yii::app()->user->getState('MyModelSearch');

		if ( isset($search) )

		{

			$model->attributes = $search;

		}

	}

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

		'model'=>$model,

	));

}



But, it won’t reset the search on page reload. You will have to do something very tricky for it.

(I’d rather add a “reset search” button in the search form …)

you can have all you database fields enabled in CGridView

First of all thanks for the replies.

I took softarks idea using states to store the values, and went on from there.

I ended up with the following solution that as far as I can tell, suits all my needs.

It might not be the cleanest solution but it works for now.

By using the $_GET[‘ajax’] you can tell if the page was loaded for the first time, because if that variable is not set it should probably show all results.

I defined an array with all the fields in my gridview, if one of those fields becomes empty it’ll remove it from the search, while still remembering all the values from the advanced search option.




public function actionNaw()

	{

		$model=new CmdbCmdb('search');

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

		

		$gridviewfields = array("online","code","channel_id","klantnaam","vestiging","straat","huisnummer","postcode","plaats","contactpersoon");

		

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

		{

		    $oldvalues = array();

		    if (Yii::app()->user->hasState('CmdbCmdbSearch'))

		    {

			$oldvalues = Yii::app()->user->getState('CmdbCmdbSearch');

		    }		    

		    

		    $newvalues = $_GET['CmdbCmdb'];

		    

		    if (isset($oldvalues))

		    {

			foreach ($gridviewfields as $field)

			{

			    if (empty($_GET['CmdbCmdb'][$field]))

			    {

				if (array_key_exists($field, $oldvalues))

					unset($oldvalues[$field]);

			    }

			}


			$values = array_merge($oldvalues,$newvalues);

		    }

		    else

		    {

			$values = $_GET['CmdbCmdb'];

		    }

		    

		    Yii::app()->user->setState('CmdbCmdbSearch',$values);

		    $model->attributes = $values;

		}

		

		if (!isset($_GET['ajax']))

		{

		    Yii::app()->user->setState('CmdbCmdbSearch',array());

		}


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

			'model'=>$model,

		));

	}