Bring User Back To Where He Was In Cgridview

R U 100% sure? I’m on a quite fast connection (localhost, dev environment), I don’t see spinner at all (TbGridView seems to be dropping this idea), but I do clearly see, that something is added to my URL (actually, not something, but exactly the same URL parts, as when CGridView is not using AJAX-update). Speeds are nearly or exactly the same, no matter if I use [color="#1C2837"]enableHistory or not. So I assume, that my CGridView dropps out of AJAX-update, when I’m using [/color][color="#1C2837"]enableHistory, only basing on my URL changes. And the fact, that when [/color][color="#1C2837"]enableHistory is turned on, I see spinner spinning in my Chrome tab’s icon.[/color]

[color="#1C2837"]But, this is side-talk, whether this works AJAX-less or am I only thinking it is. The key problem here is, that either I’m missing something, or [/color][color="#1C2837"][size=“2”]enableHistory does not work at all. I have HTML5-enabled browser and I can’t reproduce [/size][/color][color="#1C2837"][size=“2”]"[/size][/color]persist state of grid across page revisits[color="#1C2837"][size=“2”]", as I shown on my examples.[/size][/color]

The basic concept of “enableHistory” is that your browser’s address bar is updated to the exact url even when the page is updated by an ajax call.

Usually the get parameter like “Model_page=2” for pagination will not appear in your browser’s address bar when the grid is updated by ajax. But when you set “enableHistory” to true, it will appear. And every url of the ajax updated page will be recorded as a history in the browser.

This is quite convenient for users, because the click of the BACK button will bring him/her back to the exact place where he/she was before. And the user can bookmark the exact page, too.

Enabling HTML5 history for your grid is not side-talk Trejder. :)

It does what you want it to: the user can use the back button, and/or you can use the getUrlReferrer method.

Hi,

This is what I usually used on almost of all my projects. Even, I put that as gii template model+crud. I hope someday can be the standard feature for CGridView. Here is the link of My wiki

Cheers,

Daniel

PS: I welcome feedback for my wiki.

Reopening old topic…

I’m getting the entire idea of using back button, but only partial of getUrlReferrer. Somebody would have to enlighten me a little bit.

  1. Use-case scenario number one – getUrlReferrer used in "Cancel" button code in update-form for any model:

a) user is on a grid, making mess (sorting, paginating, etc.), enableHistory is true, so all his or her mess is reflected in the URL,

B) user clicks on “edit” and is redirected to update form,

c) user clicks on "Cancel", getUrlReferrer contains grid URL so redirect (and while idea) works just perfectly.

Amen! :]

  1. Use-case scenario number one – getUrlReferrer used in "Submit" button code in update-form for any model:

a) user is again in update form and getUrlReferrer again contains grid URL,

B) user clicks “Submit”, controller saves model and getUrlReferrer now contains update-form URL, not grid-one,

c) redirect after save (and whole thing) fails, as user is redirected back to update form, not to grid.

This second scenario concerns me. Do I get it right, that I don’t get it done basing on just pure, internal Yii toys? That this is my responsibility to transport correct grid URL from update-from to controller’s model save code? For example, using additional model’s field?

Trejder

I am looking into doing something like this and have just tried setting enableHistory to true in the CGridView widget. From what softark said it should update the URl but it is not happening. Have you got it working? and if so how did you do it? I’m using Firefox 22 on Ubuntu.

Thank you for any help.

Stephen

[edit]

I can see it working for the pagination but I want it to also work for the search, any ideas?

Yes, I have it working for all: sorting, pagination and filtering / searching.

All I did to enable this, was to add:


'enableHistory'=>TRUE

to definition of each CGridView, where I wanted to use it. Yii did all the rest and since enabling this, I was able to see extra additions to URL to make this working.

As for processing the results (currently I have this only for Cancel buttons, I’m working over submit buttons), I had such cancel button:


$this->widget('bootstrap.widgets.TbButton', array

(

	'label'=>'Anuluj',

	'icon'=>'remove',

	'url'=>Yii::app()->createUrl('content/manage/index'),

	'htmlOptions'=>array('style'=>'margin-left: 5px')

));

so it was always redirecting user to first page of index, no matter, what he or she was doing.

I changed above code to:


$url = Yii::app()->request->getUrlReferrer();

$url = is_null($url) ? Yii::app()->createUrl('content/manage/index') : $url;


$this->widget('bootstrap.widgets.TbButton', array

(

	'label'=>'Anuluj',

	'icon'=>'remove',

	'url'=>$url,

	'htmlOptions'=>array('style'=>'margin-left: 5px')

));

to make use of Yii’s presist state feature. This code should be self-explanatory. It checks, if there is anything in URL refferer, and if there is, it redirects user there. If not, it again redirects it to default (first page) screen.

This is very odd, that you have it working for pagination only and not for sorting or searching. This should either work for all or for non. I don’t know what to advise you. Double check, if you have AJAX update of CGridView enabled and if you don’t have more than one CGridView with “enableHistory” turned on, on one page / in one view. As docs says, that strange thing may happen in this case.