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.
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,
));
}