tonytomov/jqGrid — Issue #241: JSON Reader ID and Datatype Function
Author: cutterbl
Created: 2012-01-11 15:25:45+00:00
State: open

When creating a function **datatype**, it is often necessary to use function accessors for defining the **root**, **page**, **total** and **records** attributes within one's jsonReader configuration. This is great, because often the location of this information is in the incoming data, and not always easily defined. Programmatic reference is the better option here.

I believe the same to be true for **id**. I've recently been [writing a series on using jqGrid](http://www.cutterscrossing.com/index.cfm/jqGrid), and I am using ColdFusion on the backend. ColdFusion's json serialization of it's native query object is a bit different than the 'standard' json recordset structure, trimming the overall bit traffic by not repeating the column names with each and every record. This is a perfect example for using the **function** datatype, until you start working with the **multiselect** grid option. When working with **multiselect** it becomes very important that the grid knows which column holds the record's unique ID value.

There are two issues at play here. First, jqGrid does not allow a function accessor for the reader's **id** attribute. Second, ColdFusion does not allow one to control the column order in it's native query return. I believe I have found a very easy solution to this.

Lines 1272 - 1276, of the grid.base.js, currently read like this:

if( ts.p.keyIndex===false ) {
idn = dReader.id;
} else {
idn = ts.p.keyIndex;
}

If this can be changed to the following, then it can still be used in it's traditional manner, as well as allowing for a function accessor definition:

if( ts.p.keyIndex===false ) {
idn = $.jgrid.getAccessor(data,dReader.id);
} else {
idn = ts.p.keyIndex;
}

This will allow either of these options within a jsonReader config:

jsonReader: {
id: "ID",
// ...
id: function(obj){return $.inArray("ID",obj.data.COLUMNS);},
// ...
},

This appears to be a minor change, and I hope that it can make it into a future version of the jqGrid code.