Forcing CGridView's pagination and sort

Hi there,

When user displays my CGridView, let’s say page number 3 ordered by column number 5, and then click to display details of selected grid view item and then goes back with either pressing “Back” (tested in Firefox and IE) or by re-entering grid view’s view, he always gets first page and order by default column.

Whenever it is reasonable in second situation (page reload) I was pretty sure that in first one (going back) browser will show the page exactly as it was before leaving it. I don’t know if the fact that CGridView is being reloaded is a matter of how Firefox works (forcing refresh) or somewhere insinde Yii’s AJAX code for refreshing CGridView, but I do know, that I have to find a way to block this.

So, here is the question. What is the best practise to remember and then restore CGridView’s pagination and sorting state. Or what is the best way to block auto-refresh of CGridView? Or any other solution that wouldn’t force my user after going back record list to again click many times to go to page and sorting he was seeing (has set) before going to record details page?

You should save in the session the sort parameter and the page.

Take a look at this extension, here he saves the search field (that you too have to save, if you want to return to the same page!).

Thanks. I haven’t got idea that this is so simple! :]

You are welcome! Post your solution here so future readers (and maybe Yii develper) can include this feature in their project (or framework core… as "stateful datagrid").

Saving states in session data is one solution that may be appropriate, especially if these states are only relevant for a limited period, however if, for example, you want your user to be able to send the link to a friend so that the friend can view the exact same page (with the same page number and sort), then you need to use hash history, so that state information is saved in the hash part of the Url.

This can be done using the BBQ JQuery plugin, which is actually already used (and therefore included on your page) by the CGridView widget.

I have already made an extension of the CListView widget to support this, and have also made a version of the CGridView widget that I haven’t posted yet, since I haven’t had time to support filtering on it yet, but will be happy to do in due course.

This sounds very interesting. Can’t wait until you finish and publish extension for CGridView! :]

Hi there!

Just to let you know that I have uploaded the extensions for both CListView and CGridView, which should both support hash history for all events now. Let me know what you think and if there are any problems.

Thank you very much. I just downloaded it and I’m going to test it whenever I got time. I’ll let you know about anything related to this extension that I will find interesting.

how about using the standard widget and setting enableHistory to true ?