JqGrid getRowdata 将行中的单元格值作为字符串
JqGrid getRowdata gives cell value on a row as string
我对 jqGrid 4.6.0 有疑问
当我尝试获取行数据时,它将每个数据更改为一个字符串,我需要解析它们以获取实际的 int 或布尔值,奇怪的是,当我在自定义格式化程序中看到 rowobject 时,rowdata 似乎是正确的
这是示例代码,jsfiddle link 是我创建的示例
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}
您应该使用 getLocalRow
方法而不是 getRowData
方法来解决您的问题。重要的是要了解 getRowData
从 <td>
元素获取文本。因此,标准数据类型始终是字符串。方法 getLocalRow
只是用原始数据获取对 data
数组内部元素的引用。
再说一句:如果定义了自定义格式化程序,建议始终定义unformatter(unformat
回调)格式化程序。
一看就知道你用的是数据编辑。标准编辑将改变修改数据的类型。因此,您将遇到与以前相同的问题。免费的 jqGrid 允许通过为列指定 convertOnSave
回调来解决问题。例如,请参阅 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 并验证 data
的属性类型在编辑后是否会正确保留。
我对 jqGrid 4.6.0 有疑问 当我尝试获取行数据时,它将每个数据更改为一个字符串,我需要解析它们以获取实际的 int 或布尔值,奇怪的是,当我在自定义格式化程序中看到 rowobject 时,rowdata 似乎是正确的
这是示例代码,jsfiddle link 是我创建的示例
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}
您应该使用 getLocalRow
方法而不是 getRowData
方法来解决您的问题。重要的是要了解 getRowData
从 <td>
元素获取文本。因此,标准数据类型始终是字符串。方法 getLocalRow
只是用原始数据获取对 data
数组内部元素的引用。
再说一句:如果定义了自定义格式化程序,建议始终定义unformatter(unformat
回调)格式化程序。
一看就知道你用的是数据编辑。标准编辑将改变修改数据的类型。因此,您将遇到与以前相同的问题。免费的 jqGrid 允许通过为列指定 convertOnSave
回调来解决问题。例如,请参阅 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 并验证 data
的属性类型在编辑后是否会正确保留。