如何在Jqgrid中显示嵌套的Json对象

时间:2014-05-14 15:30:26

标签: php jquery json jqgrid

我对jqgrid很新 如何在jqgrid中显示嵌套的json对象作为单独的字段? 下面给出的是json对象的例子

[
 {
"properties":{
 "x":1,
 "y":78093,
 "closeDate":null,
 "
},
"children":[
 {
    "properties":{

       "option":null,
       "type":"",
       "client":"southface",

       "categoryA":[
          "x",
          "w"
       ],
       "facilitiesOther":null,
       "objectId":10,

       "docNo":7897,
       "Provisions":[
          "x",
          "z"
       ],


       "sponsor":"own sponsor",


       "CategoryB":[
          "e",
          "f",
          "g"

       ]

       ]
    },
    "children":null,
    "Type":"test",
    "Id":"10"
 }
  ],
  "objectType":"document",
 "objectId":"89763"
   }
  ]
经过大量研究后,我发现某处需要修改colmodel 解决这个问题的方法非常有用 提前谢谢

1 个答案:

答案 0 :(得分:1)

您应该使用jsonMap。您还应该查看jqGrid wikithis特定主题。你可以尝试这样的事情:

colNames:['Children','ID', 'Properties', 'Other','Sponsor'],
colModel: [
    {name:'children',width:100, jsonmap:"children.0", formatter: function (cellvalue) { return cellvalue.children }},
    {name:'objectId',width:100, jsonmap:"children.0", formatter: function (cellvalue) { return cellvalue.objectType }},
    {name:'properties',width:100, jsonmap:"children.0", formatter: function (cellvalue) { return cellvalue.properties.objectId }},
    {name:'other',width:100, jsonmap:"children.0", formatter: function (cellvalue) { return cellvalue.properties.other[0] }},
    {name:'sponsor',width:100, jsonmap:"children.0", formatter: function (cellvalue) { return cellvalue.properties.sponsor }}
    // and so on...
],

这显然不是最好的方法,因为您必须知道JSON上有多少条记录,并为每条记录手动完成。实际上,我不知道你怎么能自动制作它,但正如我所说,如果你在jqGrid wiki注意jsonMap,你可能会找到你想要的东西。祝你好运!