3

I have a jqGrid populated with data, but I want to change how this is shown by editing the CSS.

So far so good, but I'm trying to have a dropdownlist which, when changed, will sort the jqGrid based on the value selected.

Is there anyway to actually call the sort function programmatically?

I have tried the following which, does nothing:

$("#grid").jqGrid('setGridParam',{sortname: 'yearEdition,', sortorder: 'desc'});
$("#grid").trigger("reloadGrid");

Is there indeed a way to call this event?

JasonMHirst
  • 576
  • 5
  • 12
  • 31

3 Answers3

6

You should use sortGrid method of jqGrid:

Sorts the given colname and shows the appropriate sort icon. The same (without sorting icon) can be done using setGridParam({sortname:'myname'}).trigger('reloadGrid'). If the reload is set to true, the grid reloads with the current page and sortorder settings.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I accept what you're saying, however this "$("#list2").jqGrid().setGridMethod({sortname: 'Year,'}).trigger("reloadGrid");" simply does nothing. Am I calling it wrong? – JasonMHirst Aug 17 '11 at 08:58
  • @JasonMHirst: `$("#list2").jqGrid()` is wrong! It is the same as `$("#list2")`. Probably you wanted to write comment to another answer? Please read my answer. – Oleg Aug 17 '11 at 09:01
  • No, this is the correct question. 3nigma suggested I use the "setGridParam" which works (albeit the sort arrows). You've suggested I use "setGridParam" which doesn't work; in fact there isn't even that function in the JavaScript code. – JasonMHirst Aug 17 '11 at 09:04
  • Actually @Oleg, I've just realised that I need the "Add on" from the download customisation. Will do this and recheck. – JasonMHirst Aug 17 '11 at 09:07
  • 2
    @JasonMHirst: I suggested **to use `sortGrid` method** to sort jqGrid. I included the part of jqGrid [documentation](http://www.trirand.com/jqgridwiki/doku.php?id=wiki%3amethods) which describes *why* one should use `sortGrid` method and why another way which you use don't do the full job. – Oleg Aug 17 '11 at 09:07
  • my apologies, read your messages wrong - yes I see what you're saying now, thank you. – JasonMHirst Aug 17 '11 at 09:16
2

try this

   $("#grid").jqGrid().setGridParam({sortname: 'yearEdition,', sortorder: 
'desc'}).trigger("reloadGrid");     

here is a SO question that might help jqGrid sorting on client side

Community
  • 1
  • 1
Rafay
  • 30,950
  • 5
  • 68
  • 101
  • Hi 3nigma, thanks, yes that 'almost' works. Don't know if it's a bug in the jqGrid or not, but the 'sort arrows' don't reflect the new sort (i.e., they don't actually go to the column header) - is there somewhere to change this or is it a known bug? It's NOT that important as the column headers will be hidden, was just wondering that's all – JasonMHirst Aug 17 '11 at 08:24
0

As described by Oleg the following code will sort the grid by the yearEdition column, reload the grid and display the correct sorting icon:

$("#grid").jqGrid("sortGrid", "yearEdition", true);

If a descending sort order is required then the sortorder grid parameter must also be set e.g.

$("#grid").jqGrid()
    .setGridParam({sortorder: "desc"})
    .jqGrid("sortGrid", "yearEdition", true);

Note: As described in the API Documentation sortGrid is an Add On Grid method and will only be available if the correct download options were selected.

Rob Willis
  • 4,706
  • 2
  • 28
  • 20
  • As Anthony White mentions in the comments of the jqgrid page http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods, there appears to be an undocumented last parameter for the sortGrid method, for passing the sortorder. It works for me, while passing it using setGridParam doesn't. – morgar May 12 '17 at 18:04