I've got this code to create a grid table.
<script type="text/javascript">
var mydata = [
{State:'Azerbaijan', cont:'Asia', '2009':'10100', '2008':'61600', '2007':'39200', '2006':'31900', '2005':'31800', '2004':'29500', '2003':'18600', '2002':'58', '2001':'57'},
{State:'Bosnia and Herzegovina', cont:'Europe', '2009':'96000', '2008':'123000', '2007':'121800', '2006':'136200', '2005':'131200', '2004':'121294', '2003':'112503', '2002':'102271', '2001':'95064'},
{State:'France', cont:'Europe', '2009':'345000', '2008':'389000', '2007':'427800', '2006':'442100', '2005':'437900', '2004':'446900', '2003':'444100', '2002':'463200', '2001':'713000', '2000':'701000', '1999':'694000'},
{State:'Germany', cont:'Europe', '2009':'291800', '2008':'605876', '2007':'551030', '2006':'515539', '2005':'647934', '2004':'667800', '2003':'660800', '2002':'652800', '2001':'651600', '2000':'643545', '1999':'633803'},
{State:'Greece', cont:'Europe', '2009':'134737', '2008':'162339', '2007':'167937', '2006':'164528', '2005':'165300', '2004':'166634', '2003':'167797', '2002':'165262', '2001':'163581', '2000':'167507', '1999':'170301'},
{State:'Iceland', cont:'Europe', '2009':'810000', '2008':'760000', '2007':'515000', '2006':'328424', '2005':'272400', '2004':'271300', '2003':'265900', '2002':'263528', '2001':'242526', '2000':'225721', '1999':'221433'},
{State:'Italy', cont:'Europe', '2009':'165800', '2008':'186400', '2007':'179500', '2006':'194200', '2005':'192900', '2004':'195400', '2003':'191400', '2002':'190400', '2001':'187400', '2000':'210000', '1999':'205567'},
{State:'Netherlands', cont:'Europe', '2009':'306000', '2008':'321200', '2007':'296900', '2006':'285300', '2005':'333800', '2004':'327000', '2003':'282800', '2002':'284400', '2001':'293200', '2000':'301700', '1999':'270000'},
{State:'Norway', cont:'Europe', '2009':'1125000', '2008':'1358800', '2007':'1362000', '2006':'1383000', '2005':'1391000', '2004':'1321700', '2003':'1192000', '2002':'1044000', '2001':'1100000', '2000':'1030000', '1999':'1000000'},
{State:'Poland', cont:'Europe', '2009':'10300', '2008':'29500', '2007':'57600', '2006':'55900', '2005':'53600', '2004':'58900', '2003':'57200', '2002':'58800', '2001':'45000', '2000':'45100', '1999':'46750'}
];
jQuery("#list").jqGrid({
height: 500,
width: 672,
rowNum: -1,
shrinkToFit:false,
datatype: 'local',
colNames:['State', 'cont','1999','2000','2001','2002','2003','2004','2005','2006','2007','2008','2009'],
colModel :[
{name:'State', index:'State', width:170},
{name:'cont', index:'cont'},
{name:'1999', index:'1999', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'},
{name:'2000', index:'2000', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'},
{name:'2001', index:'2001', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'},
{name:'2002', index:'2002', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'},
{name:'2003', index:'2003', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'},
{name:'2004', index:'2004', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'},
{name:'2005', index:'2005', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'},
{name:'2006', index:'2006', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'},
{name:'2007', index:'2007', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'},
{name:'2008', index:'2008', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'},
{name:'2009', index:'2009', width:80, align:'right', formatter:'integer', sorttype:'integer', summaryType:'sum'}
],
sortname: 'State',
grouping:true,
groupingView : {
groupField : ['cont'],
groupSummary : [true],
groupColumnShow : [true],
groupText : ['<b>{0}</b>'],
groupCollapse : false,
groupOrder: ['asc']
},
});
for(var i=0;i<=mydata.length;i++)
jQuery("#list").jqGrid('addRowData',i+1,mydata[i]);
jQuery("#list").trigger("reloadGrid");
jQuery("#list").hideCol("cont");
</script>
And it works fine except the jQuery("#list").trigger("reloadGrid");
won't draw the last row. But it's still there - it wont get counted towards the sum but when you sort by another column, another row becomes last and disapears and the former shows back.
You main error is the usage of
rowNum: -1
instead of some large enough value likerowNum: 10000
.Moreover you should use
gridview: true
parameter of jqGrid anddata: mydata
instead of much more slow filling of grid with respect ofaddRowData
(moreover you use<=
in the loop instead of<
).Additionally I would recommend you to use column templates which will reduce the code and can it more readable. The result of the changes you can see here.