Tuesday, 17 February 2015

CGridView save search results when reloading the page in YII -- Make search result persist in gridview even the page is refreshed

http://www.yiiframework.com/forum/index.php/topic/35085-cgridview-save-search-results-when-reloading-page

One can store the search parameters in user session using CWebUser::setState() and read them using CWebUser::getState() afterward.

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']);
        }
        else
        {
                $searchParams = Yii::app()->user->getState('BillSearchParams');
                if ( isset($searchParams) )
                {
                        $model->attributes = $searchParams;
                }
        }
        $this->render('admin',array(
                'model'=>$model,
        ));
}
save the pagination state also 
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, the following is the solution
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. 

And as to the session timeout, you can configure it in your application configuration.
        // application components
        'components'=>array(
                ...
                'session'=>array(
                        ...
                        'timeout' => 1440,
                        ...
                ),
                ...
http://www.yiiframew...#timeout-detail 

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');
        ...

No comments:

Post a Comment