【问题标题】:jqGrid: checkbox column not reading proper json data from serverjqGrid:复选框列未从服务器读取正确的 json 数据
【发布时间】:2016-01-07 09:46:36
【问题描述】:
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
  }
]

【问题讨论】:

标签: jquery jqgrid jqgrid-formatter


【解决方案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},这是许多误解的根源。用户将看到可以更改的复选框,但您当前的代码不会以任何方式处理更改。格式化程序与网格的编辑无关。因此,即使您稍后使用某种编辑模式,更改也不会保存。

【讨论】:

    猜你喜欢
    • 2016-03-06
    • 2021-10-01
    • 2015-02-12
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2015-07-28
    • 2021-12-14
    • 2023-03-26
    相关资源
    最近更新 更多