JqG​​rid getRowdata将字符串中的单元格值提供给字符串 [英] JqGrid getRowdata gives cell value on a row as string

查看:114
本文介绍了JqG​​rid getRowdata将字符串中的单元格值提供给字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jqGrid 4.6.0有问题 当我尝试获取行数据时,它将每个数据更改为字符串,我需要解析它们以获取实际的int或boolean值,奇怪的是,当我在自定义格式化程序中看到rowobject时,rowdata似乎正确

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

这是示例代码和我创建的示例的 jsfiddle 链接

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}

推荐答案

您应该使用getLocalRow方法而不是getRowData来解决问题.重要的是要理解getRowData<td>元素获取文本.因此,数据的标准类型始终是字符串.方法getLocalRow仅使用原始数据获取对data数组内部元素的引用.

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.

再说一遍:建议定义取消格式化程序(unformat回调)格式化程序,如果一个定义了自定义格式化程序,则始终如此.

One more remark: it's recommended to define unformatter (unformat callback) formatter always if one defines custom formatter.

可以看到您正在使用数据编辑.标准编辑将在修改时更改数据类型.因此,您将遇到与以前相同的问题.免费的jqGrid可以通过为列指定convertOnSave回调来解决此问题.有关更多详细信息,请参见 Wiki 文章.另外,免费的jqGrid支持一些标准的列模板,从而简化了布尔,整数和数字的数据转换.可以在Count列中添加template: "integer"属性(请参阅模板定义(请参见演示并进行验证data的属性类型将在编辑后正确保存.

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.

这篇关于JqG​​rid getRowdata将字符串中的单元格值提供给字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆