The data loads fine into the grid, but it won\'t sort.
When I click in the table header, the sort arrows appear, but the data\'s not being sorted.
Thanks.
You code have many important errors:
colModel
contains index
properties which are different from the value of name
for the same item. It's the main bug. You don't specified any sortname
option of jqGrid so the values of index
properties will be never seen by the server. If you use loadonce: true
then index
properties must be the same as the values of name
properties. I recommend you don't include index
properties at all. In the case index
properties will be initialized with the values of name
propertiesid
property in jsonReader
: id: "0"
. One use such value sometime in case of repeatitems: true
. In the case the row will be represented in JSON input as array. So 0
value will be correct because in can be used as index in the array. In case of usage repeatitems: false
the items represented rows of data in the JSON input are objects with named properties. So You should use id: "ID"
in your case. Moreover you don't need to include in jsonReader
the properties which value are default (root:"rows"
, page: "page"
) and so on.reloadGrid
inside of loadComplete
. You should understand that loadComplete
will be executed on every reloading of the grid (event on local reloading). So it would be wrong making permanent reloading of grid. Moreover usage of reloadGrid
inside of loadComplete
is bad from another point of view. If you trigger reloadGrid
the event will be executed * immediately*, but the grid is not in processing of previous loading. So it would be more correct to trigger reloading inside of setTimeout
with some small time interval like 50
ms.height: "auto"
if you need display not so much rows and use column templates which could reduce the size of your code and simplify its management.After described above changing the could will be something like below
var myFloatTemplate = { width: 80, align: "right", sorttype: "float" };
$("#CompTable").jqGrid({
url: "BomExplosionInJsonObj.asp",
datatype: "json",
height: "auto",
colNames: ["Part", "Description", "Src", "Std Usage", "Usage Inc Scrap", "Rate Scrap", "UOM", "Item", "Unit Cost", "Stock"],
colModel: [
{name: "COMP1_PART", width: 120},
{name: "WSCOMPDESC", width: 300},
{name: "WSCOMPSRC", width: 40},
{name: "COMPUSAGE", template: myFloatTemplate},
{name: "WSGROSSQTY", width: 120, template: myFloatTemplate},
{name: "COMPRATE_SCRAP", width: 90, template: myFloatTemplate},
{name: "COMPBASIC_UNIT", width: 60},
{name: "COMP1_ITEM", width: 60},
{name: "WSCOMPUNITCOST", template: myFloatTemplate},
{name: "WSCOMPQTYSTOCK", template: myFloatTemplate}
],
jsonReader: {
repeatitems: false,
id: "ID"
},
caption: "Bom Detail",
rowNum: 10000,
autoencode: true,
loadonce: true,
sortable: true,
sortname: "COMP1_PART",
//sortorder: "desc",
loadComplete: function () {
var $self = $(this);
if ($self.jqGrid("getGridParam", "datatype") === "json") {
setTimeout(function () {
$self.trigger("reloadGrid"); // Call to fix client-side sorting
}, 50);
}
}
});
The corresponding demo is here. Local sorting works and it displays the following results
UPDATED: Starting with version 4.12.0 free jqGrid fork of jqGrid, which I develop, support new forceClientSorting: true
option. It works in combination with loadonce: true
option and allows first load all the data from the server response, then sort the data locally and only then to display page of the data. It makes the trick with reloading of grid inside of setTimeout
, started in loadComplete
, described in the answer, not needed. One need just replace the above loadComplete
code to one additional option forceClientSorting: true
. The option forceClientSorting: true
have additional two benefits:
The code you posted shows that the sorting will be done on the server not on the client. Your jqGrid will post sord
and sidx
parameters to allow you to do so as part of returning the data to the jqGrid.
Ex: C# MVC Controller code, where paging is also performed
public ActionResult GetGridData(string sidx, string sord, int page, int rows, bool _search, string filters)
{
...
var pagedQuery = wholeDataSet.OrderBy(sidx + " " + sord).Skip((page - 1) * rows).Take(rows).ToList();
...