JQuery jqgrid not sorting on client side -
the data loads fine grid, won't sort.
when click in table header, sort arrows appear, data's not being sorted.
thanks.
$("#comptable").jqgrid({ url:'bomexplosioninjsonobj.asp' , datatype: 'json' , mtype: 'get' , height: 400 , colnames:['part','description','src','std usage','usage inc scrap','rate scrap','uom','item','unit cost','stock'] , colmodel:[ {name:'comp1_part',index:'part', width:120} , {name:'wscompdesc',index:'desc', width:300} , {name:'wscompsrc',index:'src', width:10} , {name:'compusage',index:'usage', width:80, align:"right",sorttype:"float"} , {name:'wsgrossqty',index:'totusage', width:80, align:"right",sorttype:"float"} , {name:'comprate_scrap',index:'rate scrap', width:80, align:"right",sorttype:"float"} , {name:'compbasic_unit',index:'uom', width:20} , {name:'comp1_item',index:'item', width:20} , {name:'wscompunitcost',index:'unitcost', width:80, align:"right",sorttype:"float"} , {name:'wscompqtystock',index:'stock', width:80, align:"right",sorttype:"float"} ] , jsonreader: { root:"rows" , page: "page" , total: "total" , records: "records" , repeatitems: false , id: "0" } , multiselect: false , caption: "bom detail" , rownum: 10000 , autoencode: true , loadonce: true , sortable: true , loadcomplete: function() {jquery("#comptable").trigger("reloadgrid");}// call fix client-side sorting }); });
json data being returned (as read firebug).
{ "total":"1" ,"page":"1" ,"records":"2" , "rows": [ {"id":1,"wscompdesc":"zytel e101l blk moul ","wscompunitcost":7.08,"wscompsrc":"p ","wscompqtystock":75,"compbasic_unit":"kg ","compusage":0.0034,"comprate_scrap":0,"wsgrossqty":0.0034,"comp1_part":"1180019 ","comp1_item":" " }, {"id":2,"wscompdesc":"insert ","wscompunitcost":1.89,"wscompsrc":"p ","wscompqtystock":400,"compbasic_unit":"ea ","compusage":2,"comprate_scrap":0,"wsgrossqty":2,"comp1_part":"4owe195689\/iss 2 ","comp1_item":" " } ] }
you code have many important errors:
colmodel
containsindex
properties different value ofname
same item. it's main bug. don't specifiedsortname
option of jqgrid values ofindex
properties never seen server. if useloadonce: true
index
properties must same values ofname
properties. i recommend don't includeindex
properties @ all. in caseindex
properties initialized values ofname
properties- you use wrong value of
id
property injsonreader
:id: "0"
. 1 use such value sometime in case ofrepeatitems: true
. in case row represented in json input array.0
value correct because in can used index in array. in case of usagerepeatitems: false
items represented rows of data in json input objects named properties. should useid: "id"
in case. don't need include injsonreader
properties value default (root:"rows"
,page: "page"
) , on. - the next problem usage of unconditional
reloadgrid
inside ofloadcomplete
. should understandloadcomplete
executed on every reloading of grid (event on local reloading). wrong making permanent reloading of grid. usage ofreloadgrid
inside ofloadcomplete
bad point of view. if triggerreloadgrid
event executed * immediately*, grid not in processing of previous loading. more correct trigger reloading inside ofsettimeout
small time interval50
ms. - the last recommendation usage of k&r (kernighan , ritchie's) style of writing of code. it's not important style of formatting of code use in computer language , it's not important 1 find nice read. javascript has it's own rights. 1 there automatic semicolon insertion (see here example). if follow k&r never have problems automatic semicolon insertion.
- i recommend use
height: "auto"
if need display not rows , use column templates reduce size of code , simplify management.
after described above changing 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 fix client-side sorting }, 50); } } });
the corresponding demo here. local sorting works , displays following results
updated: starting version 4.12.0 free jqgrid fork of jqgrid, develop, support new forceclientsorting: true
option. works in combination loadonce: true
option , allows first load data server response, sort data locally , display page of data. makes trick reloading of grid inside of settimeout
, started in loadcomplete
, described in answer, not needed. 1 need replace above loadcomplete
code 1 additional option forceclientsorting: true
. option forceclientsorting: true
have additional 2 benefits:
- one don't see flicker after displaying of first (unsorted) grid;
- the performance of grid better, if have many rows, because displaying grid sorting. trick described in old answer displays grid, delete content (which too) , 1 displays sorted grid once more.
Comments
Post a Comment