0

I'm using jQGrid and I'm wondering if there is a way to get the current filters that have been applied to the grid I'm viewing. For example, if the grid has been sorted by a certain column (asc or desc), search parameters that may have been applied, etc.

Here is my problem: I would like to pass some parameters back to the previous grid I was viewing (or the last page) so that I can see the previous grid as it was when I navigated forward. Basically, I have a link the user will click which navigates back to the page with the previous grid they were viewing. I want to pass the filter parameters along with that link.

Overall Example:

  1. I filtered the Client grid by the client_name field in asc order.
  2. I then clicked a link which takes me to a separate page with a new grid.
  3. I want to navigate back to the Client grid I was previously on, seeing my grid with client_name filtered in asc order as it was when I had navigated away the first time.

Currently, I am able to navigate back to the Clients grid but I would like to get the filter parameters that had been applied so I can pass them with my link back.

Let me know if I'm being specific enough!

Thanks for the help!

FastTrack
  • 8,810
  • 14
  • 57
  • 78

1 Answers1

0

I would recommend to read the answer and this one. The answers shows how to solve very close problem by the usage of window.localStorage. The most important options of jqGrid which defines the state are: sortname, sortorder for sorting, search and postData (mostly postData.filters) for filtering, page for paging. Moreover if you use columnChooser you would need to use remapColumns option of jqGrid. You can include additionally take in consideration the information about current selected row/rows (selrow or selarrrow). So the exact list of the parameters which you need save or restore depends on your requirements.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I'll check those links out later on today when I get the time! – FastTrack Jun 27 '12 at 12:20
  • Oleg: I'm thinking I would like to shy away from `localStorage` since I have clients using my application who use IE7. Instead, I would like to save the jqGrid state in a MySQL DB. Is there any short way to grab the current states of: `sortname`, `sortorder`, `search`, `postData`, `postData.filters`, `page`, and `remapColumns`? Maybe something like `jqGrid('getGridParam', 'sortname')`? Thanks as always Oleg! – FastTrack Jun 27 '12 at 18:17
  • 1
    @FastTrack: You can just follow the example from [the answer](http://stackoverflow.com/a/8436273/315935). It uses `getGridParam` to get parameters `sortname`, `sortorder`, `search`, `postData`, etc. You can send the current grid state to the server by `$.ajax` instead of call of `window.localStorage.setItem`. – Oleg Jun 27 '12 at 18:32
  • Oleg: Ok, great! And I'm assuming I would set the parameters with `setGridParam`? Also, does that example cover the columns order and if each column is hidden or not? I couldn't find it from just reading over the example. Thanks! – FastTrack Jun 27 '12 at 18:46
  • Also, when I use `jqGrid('getGridParam', 'postData.filters')` to get the search parameters the user has used, it keeps returning `null`. I'm assuming I'm doing something wrong there :-) – FastTrack Jun 27 '12 at 19:00
  • 1
    @FastTrack: There are no parameter `'postData.filters'`. If you need to get `filters` property of the parameter `postData` you should use `.jqGrid('getGridParam', 'postData').filters` – Oleg Jun 27 '12 at 19:08
  • Ok, I'm getting the filters correctly with `.jqGrid('getGridParam', 'postData').filters` but do I set them the same way but with `setGridParam`? Sorry for so many questions! – FastTrack Jun 27 '12 at 19:25
  • 1
    @FastTrack: You can get **the reference** to internal object `postData` by `.jqGrid('getGridParam', 'postData')` and then set any its property like `filters` directly. No additional call of `setGridParam` is required because you will work with *reference* to `postData` used by jqGrid. – Oleg Jun 27 '12 at 19:31
  • Ok, so basically if I want to set the filters equal to a variable it would look like this: `.jqGrid('getGridParam', 'postData').filters = myFilters`? – FastTrack Jun 27 '12 at 19:46
  • 1
    @FastTrack; Yes, it's correct. You should don't forget to use `JSON.stringify` with `myFilters` objest. So the value of `filters` property must be JSON *string*. – Oleg Jun 27 '12 at 19:58