tonytomov / jqGrid

jQuery grid plugin
www.trirand.com
2.85k stars 1.2k forks source link

Stop Automatic Sorting #901

Closed Rikaelus closed 6 years ago

Rikaelus commented 6 years ago

I'm overwriting onSortCol to trigger a data reload into a local jqGrid instance, returning "stop" which I read is supposed to stop the default sorting behavior. My logic then figures out what column is being sorted and how, reloads data via AJAX, and inserts the data.

Unfortunately jqGrid still seems to be performing its own sorting after I set new data and "reloadGrid".

Normally this wouldn't be an issue but I'm imposing my own secondary and tertiary sorting on the backend and that's being overridden by jqGrid attempts to automatically sort.

Is there a way to prevent that automatic sorting from taking place, and the grid simply rendering rows in the exact order I'm providing them?

Thanks

tonytomov commented 6 years ago

Hello,

After you load the new data and reload the grid the grid sort the data (it seems you use datatype loacal or loadonce true) according to its last value in sortname and sortorder parameters. To overcome this

  1. Do not reload the grid since the data is already sorted
  2. Change the sortname according to your sort, which comes from the server
Rikaelus commented 6 years ago

Re #1: Right now I'm loading new data via: $usageReportGrid.jqGrid('clearGridData').jqGrid('setGridParam', { data: rows, rowNum: rpp }); If I don't perform a reloadGrid after that, the grid remains empty and the "viewrecords" area insists there's no results.

Re #2: The sortname already coincides with the primary sort coming from the server. It's the secondary, tertiary, etc., sorts coming from the server that are being lost when jqGrid re-sorts on only sortname.

It looks like I might have found a workaround, though. If I do $usageReportGrid.addRowData(row['id'], row, 'last'); instead, then it properly fills without needing to do a reloadGrid. Hopefully that'll continue to work and I won't find myself in another position where I have to use reloadGrid to kick it into working elsewhere.

tonytomov commented 6 years ago

This is not tested, but you can use multiSort set to true and separate the columns in sortname with commas - i.e

$usageReportGrid.jqGrid('clearGridData').jqGrid('setGridParam', { 
   data: rows, rowNum: rpp,
   multiSort : true,
   sortname : field1, field2, field3 
});
tonytomov commented 6 years ago

Feel free to reopen it if you think that is something wrong or unclear.