【问题标题】:Using key/value pairs for jqGrid cell data对 jqGrid 单元格数据使用键/值对
【发布时间】:2013-03-22 21:05:14
【问题描述】:

我有一个这样定义的 jqGrid:

$("#tableFeedbackReports").jqGrid({
            url: '/FeedbackReports/GetFeedbackReport',
            datatype: 'json',
            colNames: ['ColA', 'ColB', 'ColC', 'ColD'],
            colModel: [{ name: 'ColA', index: 'ColA', width: 60 },
                        { name: 'ColB', index: 'ColB', width: 60 },
                        { name: 'ColC', index: 'ColC', width: 60 },
                        { name: 'ColD', index: 'ColD', width: 60 }, 
/* ... and so on */

现在,当 ajax 调用返回时,它必须返回一个数组,其中包含将进入每一行的内容。

['value', 'value', 'value']

是否可以让 jqGrid 接受行数据的键/值对?

[{ 'ColA' : 'value', 'ColB' : 'value', 'ColC' : 'value', 'ColD' : 'value'}]

那么jqGrid在加载数据时,会自动将数据绑定到模型中的列上?

【问题讨论】:

    标签: javascript jqgrid


    【解决方案1】:

    查看jqGrid Wiki 上的jsonReader 选项,特别是它的repeatitems 属性。从该页面:

    repeatitems 元素告诉 jqGrid 中的数据信息 行是可重复的 - 即元素具有单元格中描述的相同标签单元格 元素。将此选项设置为 false 指示 jqGrid 在 按名称的 json 数据。这是 colModel 中的名称或描述的名称 colModel 中的 jsonmap 选项。

    他们的例子是:

    jQuery("#gridid").jqGrid({
    ...
       jsonReader : {
          root:"invdata",
          page: "currpage",
          total: "totalpages",
          records: "totalrecords",
          repeatitems: false,
          id: "0"
       },
    ...
    });
    

    它将处理以下格式的数据,带有键/值对:

    { 
      totalpages: "xxx", 
      currpage: "yyy",
      totalrecords: "zzz",
      invdata : [
        {invid:"1",invdate:"cell11", amount:"cell12", tax:"cell13", total:"1234", note:"somenote"},
        {invid:"2",invdate:"cell21", amount:"cell22", tax:"cell23", total:"2345", note:"some note"},
      ...
    

    ] }

    【讨论】:

    • 非常感谢。在这个问题上绞尽脑汁好几个小时!
    【解决方案2】:
     Tag    Description
     total  total pages for the query
     page   current page of the query
     records    total number of records for the query
     rows   an array that contains the actual data
      id    the unique id of the row
    cell    an array that contains the data for a row
    

    http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data

    root
    

    元素。这个元素描述了我们的数据从哪里开始。换句话说,这指向包含数据的数组。如果我们设置 jQuery("#gridid").jqGrid({ ... jsonReader : {root:"invdata"}, ... }); 那么返回的字符串应该是

    { 
      "total": "xxx", 
      "page": "yyy", 
      "records": "zzz",
      "invdata" : [
        {"id" :"1", "cell" :["cell11", "cell12", "cell13"]},
        {"id" :"2", "cell":["cell21", "cell22", "cell23"]},
      ...
      ]
    }
    

    所以如果你选择键值方式;单元格不应该在内容 json 字符串中,但行应该;

    jQuery("#gridid").jqGrid({
    ...
       jsonReader : {
          repeatitems: false,
       },
    ...
    });
    

    结果数据应该是:

     {"page":"1","total":1,"records":"1",
    "rows": [
        {"invid" : "1","invdate":"cell11", "amount" :"cell12", "tax" :"cell13", "total" :"1234", "note" :"somenote"},
        {"invid" : "2","invdate":"cell21", "amount" :"cell22", "tax" :"cell23", "total" :"2345", "note" :"some note"}]
    

    "id":"1""cell"关键字出,关联(key value)数组数据直接在rows关键字下;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-08
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多