Yii Framework Forum: CGridview, save search-results when reloading page - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CGridview, save search-results when reloading page still using ajax Rate Topic: -----

#1 User is offline   adde 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 30-April 12

Posted 30 August 2012 - 02:23 PM

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?
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 30 August 2012 - 09:16 PM

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

0

#3 User is offline   adde 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 30-April 12

Posted 31 August 2012 - 04:00 AM

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?
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 31 August 2012 - 05:42 AM

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.
1

#5 User is offline   adde 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 30-April 12

Posted 31 August 2012 - 05:52 AM

Works! Thank you, you're the best :)
0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 31 August 2012 - 06:06 AM

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.yiiframew...#timeout-detail
0

#7 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 879
  • Joined: 20-April 11
  • Location:India

Posted 31 August 2012 - 06:26 AM

and u can save this searches with user_id to database, and give loadsearches according to user. hows it!
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 31 August 2012 - 06:35 AM

View PostRajith R, on 31 August 2012 - 06:26 AM, said:

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

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.
0

#9 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 879
  • Joined: 20-April 11
  • Location:India

Posted 31 August 2012 - 06:40 AM

thats too grt..

i used Yii::app()->request->getUrl() to save current url, using GET. in one of my app.
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#10 User is offline   Tibor Katelbach 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 11-April 11
  • Location:Reunion Island

Posted 09 November 2012 - 03:37 AM

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
0

#11 User is offline   TinKi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 04-June 12

Posted 19 November 2012 - 07:30 AM

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

0

#12 User is offline   Porcelanosa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 04-July 10

Posted 27 October 2013 - 06:01 PM

http://www.yiiframew...eHistory-detail
0

#13 User is offline   juanruben 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 22-May 14

Posted 10 October 2014 - 01:38 PM

View PostTibor Katelbach, on 09 November 2012 - 03:37 AM, said:

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


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;
}
}
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users