Reputation: 1946
I have an issue with jqGrid 4.6.0 When I try to get the row data it changes each data to a string I need to parse them to get the actual int or boolean values what is strange is that when I see the rowobject inside a custom formatter the rowdata seems correct
Here is the sample code and jsfiddle link for the sample I created
var myformatter = function (cellval, options, rowObject)
{
// rowObject is correct here {id: 1, Name: "test1", IsActive: true, Count: 10}
var active = rowObject.IsActive;// here active is true/false which is good
var count = rowObject.Count; // here count is 10,20,30 which is good
if(active )
{
// do what ever
}
return cellval;
}
var mydata = [
{id:1, Name: "test1", IsActive: true, Count: 10},
{id:2, Name: "test2", IsActive: false, Count: 20},
{id:3, Name: "test2", IsActive: false, Count: 30} ];
var grid = $("#list").jqGrid({
datatype: "local",
data: mydata,
height: "auto",
colNames: ['id', 'Name','Is Active','Count'],
colModel :[
{name:'id', index:'id', width:55},
{name:'Name', index:'Name', width:90},
{name:'IsActive', index:'IsActive', width:90, editable: true ,formatter:myformatter},
{name:'Count', index:'Count', width:90, editable: true}
],
pager: '#pager',
rowNum:10,
rowList:[10,20,30],
sortname: 'idcustomers',
sortorder: 'asc',
viewrecords: true,
gridview: true,
caption: 'Customers',
cellEdit: true,
cellsubmit: 'clientArray',
});
var row = $('#list').jqGrid('getRowData', 1);
// row is: {id: "1", Name: "test1", IsActive: "true", Count: "10"}
// What I was expecting {id: 1, Name: "test1", IsActive: true, Count: 10}
Upvotes: 0
Views: 1341
Reputation: 222017
You should use getLocalRow
method instead of getRowData
to solve your problem. It's important to understand that getRowData
get the text from <td>
element. Thus the standard type of data is always string. The method getLocalRow
just get the reference to internal element of data
array with original data.
One more remark: it's recommended to define unformatter (unformat
callback) formatter always if one defines custom formatter.
One can see that you use editing of data. The standard editing will change the type of data on modification. Thus you will have the same problem as before. Free jqGrid allows to fix the problem by specifying convertOnSave
callback for the column. See the wiki article for more details. Additionally free jqGrid support some standard column templates, which simplifies the data conversion for Boolean, integer and numbers. On can add template: "integer"
property in the Count
column (see the template definition here) and to add template: "booleanCheckbox"
(see here). You can debug the demo for example and verify that the types of properties of data
will be hold correctly after editing.
Upvotes: 2