1

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"
            }
     });

Notice that the column 'meta.status' is in JSON dot notation and accordingly the data sent from the server is 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} ] }

The problems are of two fold:

  • Sorting does not work on columns with dot notation, here "meta.status". It does not even show the sortable icons on the column header, and nothing happens even if the header is clicked. Sorting does not work, whether loadonce is true or false.
  • If I try Searching (after setting loadonce to true) for the column meta.status (other columns without dot notation is okay), then it throws up a javascript error like this. alt text
Machavity
  • 30,841
  • 27
  • 92
  • 100
rsmoorthy
  • 2,284
  • 1
  • 24
  • 27

2 Answers2

3

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

One can fix the problem it one 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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Fantastic! Thanks much! This fixed the problem and I am too happy! Also that JS error posted (which does not happen now after the fix) was happening only if I do searching (Simple Search or Toolbar search) etc with loadonce: true. Note: Sorry for buggy js code by not quoting 'amount' – rsmoorthy Jan 10 '11 at 16:57
  • @rsmoorthy: You welcome! I was glad to help you! Because the usage of `'meta.status'` is legal in names and described here: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_dot_notation for example, I recommend you to post the problem as a bug in the http://www.trirand.com/blog/?page_id=393/bugs/ forum. Then the developer of jqGrid (Tony) could know about the problem and would probably fix it in the next release. – Oleg Jan 10 '11 at 17:24
  • Sure, Will file a bug. Also is the "jsonmap" used only one way - ie. extracting json data by jqgrid? When I do cellediting of meta.status field, jqGrid sends the field/value to server as "status" only, not as "meta.status" - ie it is not referring to jsonmap while sending data to server. The documentation seems to suggest that this is used only while retrieving the data. What do you think? Should it not be used while sending also? – rsmoorthy Jan 11 '11 at 09:40
  • @rsmoorthy: This behaviour is absolutely correct. `index` will be send to the server to identify the sorted column. If you don't define `index` for the column, the `name` will be used. `jsonmap` are used only for reading JSON data. – Oleg Jan 11 '11 at 10:24
  • @Oleg: I have exactly the same problem, but using the jsonmap exactly as stated in the answer above does not render the result for me.. My problem is exactly the same but with different attribute names. Should I create a separate question and post it for you? – Ali Dec 24 '13 at 18:16
  • @Ali: Yes, it's better to post new question with the details of your problem. You should post JavaScript code which shows how you create the grid (especially important to know `datatype`, `jsonReader`, `colModel` and whether you use `loadonce: true`). Test data which can be used to reproduce the problem are also required (at least two rows of input data). – Oleg Dec 25 '13 at 00:19
1

As a rule of thumb to avoid this problem:

  1. Ensure that your name and index values are the same

    name: 'Date', index: 'Date',
    name: 'Clicks', index: 'Clicks',
    ...
    
  2. Ensure you set something like

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

    And that when you reload the grid - you correct this to "JSON" on reload if you're using it - i.e.

    $("#yourGridID").setGridParam({datatype: 'json'}).trigger("reloadGrid");
    
  3. Lastly, ensure that you use

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

    Where you need it.

Tim
  • 2,466
  • 1
  • 21
  • 18