jqGrid - How does one configure jsonreader (for use with Jayrock)? jqGrid - How does one configure jsonreader (for use with Jayrock)? json json

jqGrid - How does one configure jsonreader (for use with Jayrock)?


No, you can't do this via jsonReader. Internally, the grid does:

        ts.p.page = data[ts.p.jsonReader.page];

...which won't work for a dotted sub-property.

Instead you'll need to fetch the grid data manually by setting datatype to a function. You can then fetch the data with $.ajax, and call grid.addJsonData when it comes back, just like the grid does, except that instead of passing the whole response you'll pass a sub-property of the response.


This post and links have been really helpful.I don't have a clear understanding on how this works yet but I thought that I would just post this to help someone ease their pain :)

This is the return JSON from getRecords:

{"id":-1,"result":{"page":"1","total":"1","records":"2","rows":[{"id":"13","invdate":"2007-10-06","name":"Client 3","amount":"1000.00","tax":"0.00","total":"1000.00","note":""},{"id":"12","invdate":"2007-10-06","name":"Client 2","amount":"700.00","tax":"140.00","total":"840.00","note":"no tax"}]}}

And this is the working code:

jQuery(document).ready(function(){     jQuery("#list4").jqGrid({         contentType: "text/plain; charset=utf-8",        datatype: function(postdata)        {            $.ajax({                url: 'http://localhost/Booga/Baba.ashx/getRecords',                data: "{}", // For empty input data use "{}",                dataType: "json",                type: "GET",                contentType: "application/json; charset=utf-8",                complete: function(response, status)                {                    if(status=='success')                    {                        var mygrid = jQuery("#list4")[0];                        var o = eval("(" + response.responseText + ")");// TODO don't use eval.  it's insecure, but older browsers support it...                        mygrid.addJSONData(o.result);                    }                }            })        },                    colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],    colModel:[        {name:'id',index:'id', width:55},        {name:'invdate',index:'invdate', width:90, jsonmap:"invdate"},        {name:'name',index:'name asc, invdate', width:100},        {name:'amount',index:'amount', width:80, align:"right"},        {name:'tax',index:'tax', width:80, align:"right"},              {name:'total',index:'total', width:80,align:"right"},               {name:'note',index:'note', width:150, sortable:false}           ],        jsonReader: {            repeatitems: false        }    });  });

By the way, does anyone know why using eval is insecure? Look at the comment on my code. I grabbed that part from the forum.asp link.


I.e. it adds a "{id:'-1','result':{ /* ... snip ... */ }}" wrapper around the working JSON.

This is a side-effect of the way JayRock handles SMD responses...the "id" is a "response identifier" and is an aid for asynchronous communications.

So, if you fire off a bunch of async requests (and don't wait around for the response), you can specify the "request ID" and JayRock will honor that...so you can line up the responses with the requests.

I hope that makes a little sense.