Page 1 of 1
CGridview, save search-results when reloading page still using ajax
#1
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?
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?
#2
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.
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,
));
}
#3
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?
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?
#4
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 ...
Note that you have to reset the page information every time the search parameters get changed.
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.
#6
Posted 31 August 2012 - 06:06 AM
Ah, fine.
And as to the session timeout, you can configure it in your application configuration.
http://www.yiiframew...#timeout-detail
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
#7
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
Wiwo inc.
| Mobile: 919995504508
#8
Posted 31 August 2012 - 06:35 AM
Rajith 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.
#9
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.
i used Yii::app()->request->getUrl() to save current url, using GET. in one of my app.
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
Wiwo inc.
| Mobile: 919995504508
#10
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
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
#11
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.
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');
...
Share this topic:
Page 1 of 1

Help













