11

I'm in the beforeRequest handler, and would like to know what the current sort column is. How can I find that?

sprugman
  • 19,351
  • 35
  • 110
  • 163

1 Answers1

19

You can examine the values of the jqGrid parameters sortname and sortorder ("desc" or "asc"). To get the parameters you can use getGridParam method:

var sortColumnName = $("#list").jqGrid('getGridParam','sortname');

and

var sortOrder = $("#list").jqGrid('getGridParam','sortorder'); // 'desc' or 'asc'
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • that's helpful, thanks. What I really want to do is add a class (either sortAsc or sortDesc) to the th of the sorted column. – sprugman Oct 27 '10 at 21:31
  • @sprugman: What do you mean under "add a class ... to the sorted column."? Do you mean the column header or all cells in the column or both? – Oleg Oct 27 '10 at 21:38
  • @Oleg: the tags in the column header. I can write some code to do it with what you've given me. (In fact, I already have.) Just checking to see if there was something built in. – sprugman Oct 27 '10 at 21:39
  • @sprugman: you can use `setLabel` method. See http://stackoverflow.com/questions/3003187/jquery-jqgrid-how-to-set-alignment-of-grid-header-cells/3006853#3006853 as an example. – Oleg Oct 27 '10 at 21:50
  • 1
    Using the setLabel method, how do I unset the old class using that method? So, first time through, the first column gets the sort-asc class. Then I click on the third column. Col 1 needs to have sort-asc removed, while col 3 has it added.... – sprugman Oct 27 '10 at 22:06
  • @sprugman: A good point! You can't. Currently `setLabel` method use only `addClass` or `css` methods and no `removeClass`. So use have to do this manually. The `setLabel` method do following (see http://github.com/tonytomov/jqGrid/blob/master/js/grid.base.js#L2904): It convert the name of the column to the column position if needed. Then it uses `var thecol = $("tr.ui-jqgrid-labels th:eq("+pos+")",$t.grid.hDiv);` to get the `` element and finally calls `$(thecol).addClass(prop);` or `$(thecol).css(prop);`. You can do the same to be most close to `setLabel` method but use `removeClass`. – Oleg Oct 27 '10 at 22:19
  • Is there a way to retrieve the original sort name? (not the current sort name). Thanks! (is this bad etiquette- asking questions in a comment? :)) – icats Jan 18 '12 at 22:15
  • @icats: Not a problem. I have enough reputation points which I can't use and I am not even a developer of jqGrid. I just help other people in my free file for the "thanks". So back to your question. You can use `$("#list").jqGrid('getGridParam','lastsort')` to get the **previous** sort order. What you mean under the "original sort name"? – Oleg Jan 18 '12 at 22:21
  • @Oleg: thanks :). By 'original' sort name I mean the sort name in the original grid options (sortname: "columnName"). – icats Jan 18 '12 at 22:40
  • @icats: You can save it somewhere at the first grid filling, in `loadComplete` for example. The code can be like `if (typeof myVarFirstSortName === 'undefined') { myVarFirstSortName = $("#list").jqGrid('getGridParam','sortname'); }` – Oleg Jan 18 '12 at 22:57