2013-01-07 29 views
1

我需要一些想法,关于如何在下面的sceaniro中实现子网格。亲子关系的JQuery Grid-SubGrid

以下是我想要在JQuery Grid和Subgrid中显示的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" 
    } ] 
} ...] 

} 

我想eache网格行显示“联系人”和与网格相关联的子网应显示“actionSet”集合。

当网格中的特定行被选中时,我不想进行服务器调用来获取关联的动作,因为它们已经全部出现在“动作集”中。

我已经得到了Grid的工作,很好地显示了“Contacts”,但是我在实现子网格的时候感到困惑,因为它如何获取数据,就像它在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数据? 这是如何实现的?

+0

我想我可以建议你一些方法来解决问题,但有一两件事对我来说似乎有些奇怪:您发布的JSON所载资料不存在'的id'数据每一行。而且我不明白网格中'id'列的价值。你打算为用户显示“id”还是仅用于内部目的?在最后一种情况下,您可以删除'id'列和'sortname:'id''参数。 JSON输入中的'id'属性将用于设置表示网格行(HTML表格)的''id''属性''。 – Oleg

+0

嗨奥列格,我会删除列中的id,这是有道理的,因为它应该用于内部目的,但是我关于子网格的问题仍然存在,以及我真正在寻求什么想法。我已阅读你的答案,其他职位,他们都很棒。我希望能从这一方面得到一些方向。 – Harbir

+0

我会为'rows'数组的每个元素添加'id'属性,并发布与修改后的数据相对应的答案(稍后)。 – Oleg

回答

4

我建议您将actionSet的信息保存在一个对象中,以便日后轻松访问。例如,您可以使用userData参数并填充beforeProcessing中的JSON数据的userdata部分。创建子网格可以按照the answeranother 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" 
       } 
      ] 
     } 
    ] 
} 
+0

嗨奥列格,谢谢你的答复。我试过了,子网格显示不正确。 请允许我看看您使用的数据。 当我通过放入警报进行调试时,发现在函数“beforeProcessing”中,“item.id”未定义。 可能是您的代码和我的代码之间的数据差异。我附上了上面的图片。 – Harbir

+0

@Adofo:我之前写过你(请参阅我对你的问题的评论)关于包含'id'属性的要求。我在演示中使用过。见[这里](http://www.ok-soft-gmbh.com/jqGrid/Adofo.json)。 – Oleg

+0

太好了,谢谢你,我会给你一个去,让你知道.. – Harbir