JqGrid Sort or search does not work with json note-labeled columns

I have this jqGrid:

$("#report").jqGrid( { url: '/py/db?coll=report', datatype: 'json', height: 250, colNames: ['ACN', 'Status', 'Amount'], colModel: [ {name:'acn', sortable:true}, {name:'meta.status', sortable:true}, {name:'amount'} ], caption: 'Show Report', rownumbers: true, gridview: true, rowNum: 10, rowList: [10,20,30], pager: '#report_pager', viewrecords: true, sortname: 'acn', sortorder: "desc", altRows: true, loadonce: true, mtype: "GET", rowTotal: 1000, jsonReader: { root: "rows", page: "page", total: "total", records: "records", repeatitems: false, id: "acn" } }); 

Please note that the column "meta.status" is in JSON notation and, accordingly, the data sent from the server looks like this:

 {"page": "1", "total": "1", "records": "5", "rows": [ {"acn":1,"meta": {"status":"Confirmed"}, "amount": 50}, {"acn":2,"meta": {"status":"Started"}, "amount": 51}, {"acn":3,"meta": {"status":"Stopped"}, "amount": 52}, {"acn":4,"meta": {"status":"Working"}, "amount": 53}, {"acn":5,"meta": {"status":"Started"}, "amount": 54} ] } 

Tasks consist of two times:

  • Sorting does not work in dotted notation columns, here is "meta.status". It doesn't even display the sorted icons in the column heading, and nothing happens even if you click the heading. Sorting does not work whether loadonce is true or false.
  • If I try to search (after setting loadonce to true) for the meta.status column (other columns without dot notation are fine), then it causes a javascript error like this. alt text
+1
source share
2 answers

After changing the definition of the last column from {name:amount} to {name:'amount'} I could reproduce your problem: sorting by "status" does not work, but I could not see the error message (see demo ).

You can fix this problem to change the definition of the second column from

 {name:'meta.status', sortable:true} 

to

 {name:'status', sortable:true, jsonmap: "meta.status"} 

See the fixed demo here .

+3
source

Typically, to avoid this problem:

  • Make sure your name and index match

     name: 'Date', index: 'Date', name: 'Clicks', index: 'Clicks', ... 
  • Make sure you install something like

     $("#jqGrid").setGridParam({datatype: 'local'}); 

    And when you reload the grid - you fix it to "JSON" on reboot if you use it - i.e.

     $("#yourGridID").setGridParam({datatype: 'json'}).trigger("reloadGrid"); 
  • Finally, make sure that you use

     name: 'Date', index: 'Date', sortable:true 

    Where do you need it.

+1
source

Source: https://habr.com/ru/post/971227/


All Articles