Bring User Back To Where He Was In Cgridview

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:




    array(

        'class'=>'CButtonColumn',

        'updateButtonUrl'=>function($data){

            return Yii::app()->createUrl('/edit/path',

                    array('id'=>$data->id, 'returnUrl'=>Yii::app()->request->url));

        },

        ....

    ),



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

        }



Dear Friend

The following is one example.

I hope this is what you expect.

Model:Wage,

Controller:WageController.

WageController.php.




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

}



Now you will exactly land from where you came.

It retains previous sorting and page as well.

The only drawback here is ugly looking url.

Regards.

And, you may consider using ‘enableHistory’ property of CGridView.

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

Maybe my xcrudcontroller extension is an option for you. It helps you to return to the same grid page after a view/edit action.

@Mike

Inspiring extension. I love the idea of "searchModel".

Thanks. I’ve used it many times to build very advanced search pages. It’s a really powerful and yet simple pattern.

I agree. It will surely help us think clear and better with the models.

Probably you’ve already tweaked gii to generate the searchModel skeleton along with the main model.

No, sorry. I almost never use gii. :)

Oh, really? So you say that you create your CActiveRecord model file manually?

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.

Yup. Or i copy an existing one. I’m a fast typer ;).

But we should no longer capture Trejders thread with OT stuff i think.

I am a total fan of that on my local development machine. ;)

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.

Oh, come on, jacmoe. You are an experienced moderator. Please don’t continue the OT talking but bring Trejder back to the thread which he has started.

/* I agree with you on Gii. */

I change the group and give group write access. Because I am in the www-data group too, all is well. Then they don’t need to be world writable.

Only for my dev machine, of course.

<edit>

I don’t use Gii very often either.

</edit>

Er. sorry Softark :lol:

I agree that enabling HTML5 history would be a good idea, and passing a returnUrl in the data would also work fine I think.

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

Thanks!

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]

What you think you are seeing is the grid without Ajax, but you are actually mistaken. :)

If you are using a slow db connection you can see that the spinner is spinning, indicating that it is Ajax/HTML5 which changes the url.