In default, auto-generated Yii application, when user click "edit" in CGridView on any other page that first, he or she is returned page to page one, after edit is done.
Is there any nice way to remember somehow, on which page user was and after edit (or any other operation) to bring him or her back to that page?
I can’t do this by simple redirect to previous URL, because in default implementation page and sorting operations are done via AJAX update, so previous URL doesn’t change and always points to first page.
Has anyone ever deal with that and have any nice solution? Is changing CGridView to use non-AJAX paging and sorting and then re-using previous URL the only solution here?
Finally, the design question – do you think, this functionality should be implemented? Have you ever met customer complying, that he always returns to first page, while he was editing record on page forty for example?
I tend to cheat by simply opening up the edit page in a new tab, which the user can close when they’ve finished. A nicer solution might be something like this:
The return URL should remain in the URL bar of the edit page as the user edits the record, so when you redirect, you can use it to send them back to the grid view page. The button in the grid should be updated every time the grid is reloaded, so the return URL should hold the details of the filters and page number that the user had applied.
The only downside is that your edit page will have an ugly URL.
EDIT
To clarify, the redirection will look something like this:
if ($model->save())
{
$this->redirect(
Yii::app()->request->getQuery('returnUrl', $this->createUrl('/grid/path'));
}
public function actionAdmin()
{
$model=new Wage('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Wage']))
{
$model->attributes=$_GET['Wage'];
}
Yii::app()->user->setState('grid',$_GET);//saving all the get parameters into session.
$this->render('admin',array(
'model'=>$model,
));
}
views/wage/view.php.
Just append the following in view.php
if(Yii::app()->user->hasState("grid"))
{
$grid=Yii::app()->user->getState('grid');
$route=array("wage/admin");
echo CHtml::link("Return to Grid",array_merge($route,$grid));
Yii::app()->user->setState("grid",null);
}
I also never use Gii, but I do use yiic to generate active record models. Using Gii means I have to configure the directories to allow write access to the PHP user, which I’m not a fan of.
Do you grant write access to the PHP user, make the directory world writable or change the owner of the directory? Just curious in case I do start using Gii in future.
@everyone: Thanks for a wonderful conversation, even though it is a bit OT at some points. But it is always good to have some noise around own topic! :]
And thanks for all inspiring ideas and solutions. I’ve been out of computer for some time, but I surely evaluate each idea posted here, once I’m back to the stuff.
How this is supposed to be working? What "persist state of grid across page revisits" means?
I’ve set some non-default sorting and went to page two on my grid, then I called “index” action again (page revisit?) and there I’m back, on first page, with default sorting. How is “state persisted” in this case?
Again. [size=“2”]I’ve set some non-default sorting and went to page three on my another grid (both with enableHistory turned on), then I[/size][size=“2”] clicked “edit” button in button column and then saved model, which redirected me back to index page with my grid. Again, I’m back on page one with default sorting.[/size]
[size="2"]Is this really working? The only thing I noticed, when using [/size]enableHistory,[size="2"] was drop of AJAX-based pagination and sorting. These operations seems to be based on page-refresh, not AJAX, when using [/size]enableHistory[size="2"].[/size]
[size=“2”]What am I missing here? I’m using Chrome 26 (newest). I think, it supports HTML5 (lol)![/size]