Hello, I use CGridview and ajax search-functionality in my webapp. When I have searched for something and reload the page or click on a link and then move back one step all the search-criterias is reset. Is it possible to "save" the criterias so the search-results is still there?
I guess it should work if I remove ajax and use urls instead but I was wondering if it possible to do this while keeping ajax?
The pagination information comes in as a query string named ‘SomeModel_page’ where ‘SomeModel’ refers to your actual model name. And it is handled directly by the grid view widget.
So, maybe this …
public function actionAdmin()
{
$model = new Bill('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Bill']))
{
$model->attributes = $_GET['Bill'];
Yii::app()->user->setState('BillSearchParams', $_GET['Bill']);
// reset the page information
Yii::app()->user->setState('BillPage', null);
}
else
{
$searchParams = Yii::app()->user->getState('BillSearchParams');
if ( isset($searchParams) )
{
$model->attributes = $searchParams;
}
}
if (isset($_GET['Bill_page']))
{
Yii::app()->user->setState('BillPage', $_GET['Bill_page']);
}
else
{
$page = Yii::app()->user->getState('BillPage');
if ( isset($page) )
{
$_GET['Bill_page'] = $page;
}
}
$this->render('admin',array(
'model'=>$model,
));
}
Note that you have to reset the page information every time the search parameters get changed.
I found that if you once in session will go to some page and/or put filter params, it will be saved even if you will reeneter the page from other admin pages. I think it should be fixed.
The way is easy - reset session params, when you enter outside controller.
public function actionAdmin()
{
if (!isset($_SERVER['HTTP_REFERER'])or(!strpos($_SERVER['HTTP_REFERER'], '_ControllerName_'))) //change _ControllerName_ to your controller page
{
Yii::app()->user->setState('BillSearchParams', null);
Yii::app()->user->setState('BillPage', null);
}
$model = new Bill('search');
...