jqGrid:复选框列没有从服务器读取正确的json数据

时间:2016-01-07 09:46:37

标签: jquery jqgrid jqgrid-formatter

grid.jqGrid({
    datatype: "json",
    url: "Industry.aspx/GetAllRecords",
    datatype: "json",
    mtype: 'POST',
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
    serializeGridData: function (posData) {
        //alert(JSON.stringify(posData.d));
        return JSON.stringify(posData);
    },
    colNames: ['', 'Code', 'Description', 'Description2', 'Active'],
    colModel: [
        { name: 'action', index: 'action', width: 45, sortable: false },
        { name: 'IndustryCd', width: 80 },
        { name: 'IndustryDs', width: 200 },
        { name: 'IndustryDs2', width: 200 },
        { name: 'active', width: 50,
            editoptions: { value: '1:0' },
            formatter: 'checkbox', formatoptions: { disabled: false}
        }
    ],
    height: 'auto',
    gridview: true,
    loadonce: false,
    id:'IndustryCd',
    jsonReader: {
        repeatitems: false,
        root: function (obj) {
            alert(obj.d);
            return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d;
        },
        page: function (obj) { return 1; },
        total: function (obj) { return 1; },
        records: function (obj) { return obj.length; }
    },
    loadComplete: function () {

    }
}); 

JSON数据

d=[
  {
    "IndustryCd": "1",
    "IndustryDs": "Manufacture",
    "IndustryDs2": "",
    "Active": true
  },
  {
    "IndustryCd": "2",
    "IndustryDs": "Sales",
    "IndustryDs2": "",
    "Active": false
  }
]

1 个答案:

答案 0 :(得分:1)

我在代码/数据中看到了一些问题:

  1. JavaScript区分大小写。您必须在colModel和输入数据中使用相同的名称(将"Active": true"Active": falsename: 'active'中的colModel进行比较。
  2. 您指定了editoptions: { value: '1:0' },但在输入数据中使用了truefalse,而不是值10
  3. 您使用id:'IndustryCd',作为jqGrid的选项。正确的位置将在jsonReader内。
  4. 您的输入数据不包含任何分页信息。因此,我可以假设您没有实现数据的服务器端分页。您应该使用loadonce: true通知jqGrid它应该加载整个数据并进行本地分页。
  5. 您创建的网格不包含pagertoppager参数。你没有在问题中写道jqGrid的哪个版本以及你使用的jqGrid的哪个分支。如果您不使用free jqGrid fork,则可以设置默认值rowNum: 20,并且只能看到从服务器返回的前20行。用户将无法更改页面并显示其余数据。如果无法升级到免费的jqGrid,则应添加rowNum一些足够大的值(如rowNum: 10000)。
  6. 您使用formatter: 'checkbox', formatoptions: { disabled: false},这是许多误解的根源。用户将看到可以更改的复选框,但您当前的代码不会以任何方式处理更改。格式化程序与编辑网格无关。因此,即使稍后使用某种编辑模式,也不会保存更改。
相关问题