CGridview, save search-results when reloading page


(Andreas Furst) #1

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?


(Softark) #2

Hi adde,

You 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,

	));

}




(Andreas Furst) #3

Thanks!

Is it possible to also save the pagination state? If I go to page 4 and then reload the page I get back to page one.

btw, where do I change the session expiration-time?


(Softark) #4

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.


(Andreas Furst) #5

Works! Thank you, you’re the best :)


(Softark) #6

Ah, fine. :)

And as to the session timeout, you can configure it in your application configuration.




	// application components

	'components'=>array(

		...

		'session'=>array(

			...

			'timeout' => 1440,

			...

		),

		...



http://www.yiiframework.com/doc/api/1.1/CHttpSession#timeout-detail


(Traprajith) #7

and u can save this searches with user_id to database, and give loadsearches according to user. hows it!


(Softark) #8

Um, I think there’s no need to make your own trick for it.

You just have to use CDbHttpSession for "session" application component to store/restore every user session state to/from database.


(Traprajith) #9

thats too grt…

i used Yii::app()->request->getUrl() to save current url, using GET. in one of my app.


(Oceatoon) #10

Hi

this was the good approach

but I noticed a conflict when trying the click back to page 1 in a Cgrid

once we went forward in pagination , The GET[‘Model_page’] not being empty anymore it’s the user->State

since The createUrl doesn’t generate any GET param for the first page link it loops on the last state.

if Model_page is set on page one as well it would do the trick

any ideas on how this could be forced ?

Thanks

Tibor


(Info) #11

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

	...




(Public Mce) #12

http://www.yiiframework.com/doc/api/1.1/CGridView#enableHistory-detail


(Juanruben) #13

This is a little bit later (2 years), but if someone is trying to solve this issue, I did it with the following code (my Model’s name is ‘Existence’):

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


    {


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


    }


    else


    {


            $page = Yii::app()->user->getState('ExistencePage');


            if (isset($page))


            {


                    $_GET['Existence_page'] = $page;


            }





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


			{


	            $page = Yii::app()->user->setState('Existence_page', 1);


                $_GET['Existence_page'] = $page;


	        }


    }