I am trying to pass the colModel and columns from struts action. Just like in the question
jqGrid and dynamic column binding
I am not sure what I am missing. Please Help. Spent quite some time trying to get it right.
jsp:
<script type="text/javascript">
$(document).ready(function(){
$.ajax(
{
type: "POST",
url: "interFinalTbaAction.action",
data: "",
dataType: "json",
success: function(result){
colD = result.couponStripList;
colN = result.columnNames;
colM = result.colModelList;
jQuery("#dataGrid").jqGrid({
jsonReader : {
repeatitems: false,
root:"rootVar",
cell: "",
id: "0"
},
url: 'SomeUrl/Getdata',
datatype: 'jsonstring',
mtype: 'POST',
datastr : colD,
colNames:colN,
colModel :colM,
loadComplete: function(data){alert('loaded');},
loadError: function(xhr,status,error){
alert('error');
}
})
},
error: function(x, e){
alert(x.readyState + " "+ x.status +" "+ e.msg);
}
});
});
</script>
<h2>Inter Final Prices</h2>
<table id="dataGrid">
</table>
</html>
The JSON that my action retuns is
{
"colModelList": [
{
"index": "prceCM",
"jsonmap": null,
"key": false,
"name": "prceCM",
"resizeable": true,
"search": true,
"sortable": false,
"title": "03-01-11",
"width": 300
},
{
"index": "prceCMPlusOne",
"jsonmap": null,
"key": false,
"name": "prceCMPlusOne",
"resizeable": true,
"search": true,
"sortable": false,
"title": "04-01-11",
"width": 300
},
{
"index": "prceCMPlusTwo",
"jsonmap": null,
"key": false,
"name": "prceCMPlusTwo",
"resizeable": true,
"search": true,
"sortable": false,
"title": "05-01-11",
"width": 300
},
{
"index": "prceCMPlusThree",
"jsonmap": null,
"key": false,
"name": "prceCMPlusThree",
"resizeable": true,
"search": true,
"sortable": false,
"title": "06-01-11",
"width": 300
},
{
"index": "coupon",
"jsonmap": null,
"key": false,
"name": "coupon",
"resizeable": true,
"search": true,
"sortable": false,
"title": null,
"width": 300
}
],
"columnNames": [
"prceCM",
"prceCMPlusOne",
"prceCMPlusTwo",
"prceCMPlusThree",
"coupon"
],
"couponStripList": {
"rootVar": [
{
"coupon": 5.0,
"prceCM": "Not Available",
"prceCMPlusOne": "Not Available",
"prceCMPlusThree": "Not Available",
"prceCMPlusTwo": "Not Available"
},
{
"coupon": 5.5,
"prceCM": "Not Available",
"prceCMPlusOne": "Not Available",
"prceCMPlusThree": "Not Available",
"prceCMPlusTwo": "Not Available"
},
{
"coupon": 6.0,
"prceCM": "Not Available",
"prceCMPlusOne": "Not Available",
"prceCMPlusThree": "Not Available",
"prceCMPlusTwo": "Not Available"
},
{
"coupon": 6.5,
"prceCM": "Not Available",
"prceCMPlusOne": "Not Available",
"prceCMPlusThree": "Not Available",
"prceCMPlusTwo": "Not Available"
},
{
"coupon": 7.0,
"prceCM": "Not Available",
"prceCMPlusOne": "Not Available",
"prceCMPlusThree": "Not Available",
"prceCMPlusTwo": "Not Available"
}
]
},
"deliveredDataTimestamp": null,
"requestedTimestamp": null
}
Thanks.
In my tests your code worked. Nevertheless, because the subject of your question is interesting for many jqGrid users, I deceide to write you some small errors and optimization in your code and JSON data.
The first and the most important problem is with the ids of items. The setting
id:"0"inside ofjsonReaderis wrong. It can be used only if the items of data are array and not objects with named properties (repeatitems:false). Currently as ids of rows will be used integers 1,2,… I strictly recommend you include id information in the items ofrootVarof JSON data.Next problem. The property
"title": "03-01-11"is wrong. The “title” property ofcolModelis boolean, so it should be changed to"title": true. Close problem: the propertyresizableyou use asresizeablewhich is probably more correct in the English, but it will be ignored by jqGrid.Now small optimizations:
datatype:'jsonstring', datastr:colDtodatatype: 'local', data: colD.rootVargridview: trueparameter.url: 'SomeUrl/Getdata', andmtype: 'POST', will be ignored in case ofdatatype:'jsonstring'ordatatype:'local'. So you should just remove the parameters of jqGrid.jsonmapwill not used in your data model I suggest you to removed it from the JSON data.labelproperty of thecolModel. In the case you will no more needcolNames(columnNamesinside your data).The original your demo you can see here (I made only the changes of
typeto `type:”GET” beacuse I have no active server components and saved the JSON as a text file). The same demo after the modifications which I suggested is here.The main restriction of the way is that all data will be local. So you can use local sorting, filtering and paging, but if you do want have server side sorting, filtering and paging than you have to include more additional code in your jqGrid.
The resulting code which I suggest you is:
the corresponding JSON data could be for example following