sainath
sainath

Reputation: 203

jqGrid getRowData returns wrong result

I have jqGrid as follows..

$('#addPGrid').button();
    jQuery("#pGrid").jqGrid({ 
    datatype: "local", 
    height: 400, 
    width:500,
    ondblClickRow:dblClck,
    colNames:['PName','PValue',"pid","pValue"], 
    colModel:[ {name:'pName',index:'pName', width:140}, {name:'pValue',index:'pValue', width:100}, {name:'pId',index:'pId', width:1, hidden:true}, {name:'pValue',index:'pValue', width:1, hidden:true}],
    caption: "FP" });

Each time data is to be entered, I create a jsonObject,and add it to JqGrid as follows,

var jsonObj = [];
jsonObj.push({pName: pNameP, pValue:pValuePlForGrid , pId:pId, dValue:dValueName});
jQuery("#pGrid").jqGrid('addRowData',1,jsonObj[0]);

I'm trying to get the data from grid in this manner,

 for(var u=1;u<=jQuery('#pGrid').jqGrid('getGridParam','records');u++)
          {
          alert(u);
           var fg = jQuery("#pGrid").jqGrid('getRowData',u);
           alert(fg.pName+"   "+fg.pValue+"   "+fg.pId+"   "+fg.dValue);
           }

Only the rowId 1's elements are shown, remaining are shown as undefined..

plzhelp..!!

Upvotes: 1

Views: 2013

Answers (1)

Justin Ethier
Justin Ethier

Reputation: 134255

Your problem is in the call to addRowData:

.jqGrid('addRowData',1,jsonObj[0]);

You are specifying a row ID of 1 so each row has the same ID - hence all of the other rows except 1 are undefined.

According to the documentation for addRowData:

Parameters

rowid, data, position, srcrowid

Description

Inserts a new row with id = rowid containing the data in data (an object) at the position specified (first in the table, last in the table or before or after the row specified in srcrowid). The syntax of the data object is: {name1:value1,name2: value2…} where name is the name of the column as described in the colModel and the value is the value. This method can insert multiple rows at once. In this case the data parameter should be array defined as [{name1:value1,name2: value2…}, {name1:value1,name2: value2…} ] and the first option rowid should contain the name from data object which should act as id of the row. It is not necessary that the name of the rowid in this case should be a part from colModel.

So to solve your problem, when you call addRowData you can either use a variable that is incremented each time, or you can set rowid to the name from the data object that should be used as the row ID.

Does that help?

Upvotes: 1

Related Questions