json - JQuery Grid-SubGrid 用于父子关系

标签 json jquery jqgrid

我需要一些关于如何在以下场景中实现子网格的想法。

以下是我想要在 JQuery 网格和子网格中显示的 json 数据。 基本上我得到一个名为“Contact”的对象,它有一个名为“actionSet”的集合。

{
 "total" : "10",
 "page" : "1",
 "records" : "78",
 "rows" : [ {
   "comment" : null,
   "givenName" : "Contact A",
   "familyName" : "A",
   "actionSet" : [ {
       "actionID" : 1,
       "actionDueDate" : "2012-12-08",
       "actionNote" : "Action 1"
       }, {
       "actionID" : 2,
       "actionDueDate" : "2012-12-08",
       "actionNote" : "Action 2"
  } ]
 }    ...]

}

我希望每个网格行显示“联系人”,并且与网格关联的子网格应显示“actionSet”集合。

当选择网格中的特定行时,我不想进行服务器调用来获取关联的操作,因为它们已经存在于“actionSet”中。

我已经让网格正常工作,很好地显示了“联系人”,但是在实现子网格时我感到困惑,如何获取它的数据,因为它已经在 json 中可用。

jq(function() {
 jq("#grid").jqGrid({
 url:'/smallworks/project/getall.do',
 datatype: 'json',
 mtype: 'GET',
   colNames:['Id', 'First Name', 'Last Name'],
   colModel:[
     {name:'id',index:'id', width:55,editable:false,editoptions:   {readonly:true,size:10},hidden:true},
   {name:'givenName',index:'givenName', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
   {name:'familyName',index:'familyName', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}}
  ],
  postData: {
  },
  rowNum:20,
  rowList:[20,40,60],
  height: 200,
  autowidth: true,
  rownumbers: true,
  pager: '#pager',
  sortname: 'id',
  viewrecords: true,
  sortorder: "asc",
  caption:"Contacts",
  emptyrecords: "Empty records",
  loadonce: false,
  loadComplete: function() {
  },

这可以实现吗? 我是否需要专门为子网格解析 JSON 数据? 如何实现这一目标?

最佳答案

我建议您将 actionSet 中的信息保存在一个对象中,以便以后轻松访问。例如,您可以使用 userData 参数并在 beforeProcessing 内填充 JSON 数据的 userdata 部分。您可以按照the answer创建子网格或another one .

The demo演示实现方法:

enter image description here

它使用以下代码

var mainGridPrefix = "s_";

$("#grid").jqGrid({
    url: "Adofo.json",
    datatype: "json",
    colNames: ["First Name", "Last Name"],
    colModel: [
        { name: "givenName" },
        { name: "familyName" }
    ],
    cmTemplate: {width: 100, editable: true, editrules: {required: true},
        editoptions: {size: 10}},
    rowNum: 20,
    rowList: [20, 40, 60],
    pager: "#pager",
    gridview: true,
    caption: "Contacts",
    rownumbers: true,
    autoencode: true,
    height: "100%",
    idPrefix: mainGridPrefix,
    subGrid: true,
    jsonReader: { repeatitems: false },
    beforeProcessing: function (data) {
        var rows = data.rows, l = rows.length, i, item, subgrids = {};
        for (i = 0; i < l; i++) {
            item = rows[i];
            if (item.actionSet) {
                subgrids[item.id] = item.actionSet;
            }
        }
        data.userdata = subgrids;
    },
    subGridRowExpanded: function (subgridDivId, rowId) {
        var $subgrid = $("<table id='" + subgridDivId + "_t'></table>"),
            pureRowId = $.jgrid.stripPref(mainGridPrefix, rowId),
            subgrids = $(this).jqGrid("getGridParam", "userData");

        $subgrid.appendTo("#" + $.jgrid.jqID(subgridDivId));
        $subgrid.jqGrid({
            datatype: "local",
            data: subgrids[pureRowId],
            colNames: ["Due Date", "Note"],
            colModel: [
                { name: "actionDueDate", formatter: "date", sorttype: "date" },
                { name: "actionNote" }
            ],
            sortname: "actionDueDate",
            height: "100%",
            rowNum: 10000,
            autoencode: true,
            autowidth: true,
            jsonReader: { repeatitems: false, id: "actionID" },
            gridview: true,
            idPrefix: rowId + "_"
        });
    }
});

更新:演示中使用的 JSON 数据如下所示。我添加了 jqGrid 所需的 id 属性。我使用 actionID 作为子网格的 id:

{
    "total": "10",
    "page": "1",
    "records": "78",
    "rows": [
        {
            "id": 10,
            "comment": null,
            "givenName": "Contact A",
            "familyName": "A",
            "actionSet": [
                {
                    "actionID": 1,
                    "actionDueDate": "2012-12-08",
                    "actionNote": "Action 1"
                },
                {
                    "actionID": 2,
                    "actionDueDate": "2012-12-09",
                    "actionNote": "Action 2"
                }
            ]
        },
        {
            "id": 20,
            "comment": null,
            "givenName": "Contact B",
            "familyName": "B",
            "actionSet": [
                {
                    "actionID": 3,
                    "actionDueDate": "2012-12-11",
                    "actionNote": "Action 3"
                },
                {
                    "actionID": 4,
                    "actionDueDate": "2012-12-10",
                    "actionNote": "Action 4"
                }
            ]
        }
    ]
}

关于json - JQuery Grid-SubGrid 用于父子关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14194754/

相关文章:

javascript - 如何使用 Javascript 或 jQuery 正确编码以下 URL?

jquery - 将数组作为 postData 参数发送

jqgrid - 停止在 jqgrid 中调整列大小

java - 使用 JSON 对象作为负载向 REST API 发出 POST 请求

ios - AFNetworking 2.0、AFJSONResponseSerializer、AFHTTPResponseSerializer 和并发

javascript - 滚动过去的元素时更改固定标题的背景

jquery - 如果使用jquery验证和signature_pad输入签名,如何添加检查?

javascript - 在 ajax 调用中填充 JqGrid

android - 是否可以使用 JSON 在 LibGDX 的 TextButtonStyle 中使用两种字体?

php - Symfony2 - 如何在 findBy 中搜索 json_array 字段