2012-12-07 36 views
4

我試圖讓嵌套子網格使用的jqGrid與本地數據的工作。我搜索了很多,但一直沒有找到解決辦法。這裏是我的代碼的簡化示例:的jqGrid:問題加載嵌套子電網與地方的數據類型

var mainGridData = 
    [ 
     // main grid data 
     { id: "m1", col1: "11", col2: "12" }, 
     { id: "m2", col1: "21", col2: "22" }, 
     { id: "m3", col1: "31", col2: "32" } 
    ]; 
var firstSubgrid = 
    { 
     m1: [ 
      // data for subgrid for the id=m1 
      { id: "s1a", c1: "aa", c2: "ab", c3: "ac" }, 
      { id: "s1b", c1: "ba", c2: "bb", c3: "bc" }, 
      { id: "s1c", c1: "ca", c2: "cb", c3: "cc" } 
     ], 
     m2: [ 
      // data for subgrid for the id=m2 
      { id: "s2a", c1: "xx", c2: "xy", c3: "xz" } 
     ] 
    }; 
var secondSubgrid = 
    { 
     s1a: [ 
      // data for subgrid for the id=m1 
      { id: "2s1a", d1: "2aa", d2: "2ab", d3: "2ac" }, 
      { id: "2s1b", d1: "2ba", d2: "2bb", d3: "2bc" }, 
      { id: "2s1c", d1: "2ca", d2: "2cb", d3: "2cc" } 
     ], 
     s2a: [ 
      // data for subgrid for the id=m2 
      { id: "2s2a", d1: "xx", d2: "xy", d3: "xz" } 
     ] 
    }; 


//------------ 
$("#grid").jqGrid({ 
    datatype: 'local', 
    data: mainGridData, 
    colNames: ['Column 1', 'Column 2'], 
    colModel: [ 
     { name: 'col1', width: 200 }, 
     { name: 'col2', width: 200 } 
    ], 
//Subgrid1... 
    subGrid: true, 
    subGridRowExpanded: function (subgridDivId, rowId) { 
     var subgridTableId = subgridDivId + "_t"; 
     $("#" + subgridDivId).html("<table id='" + subgridTableId + "'></table>"); 
     $("#" + subgridTableId).jqGrid({ 
      datatype: 'local', 
      data: firstSubgrid[rowId], 
      colNames: ['Col 1', 'Col 2', 'Col 3'], 
      colModel: [ 
       { name: 'c1', width: 100 }, 
       { name: 'c2', width: 100 }, 
       { name: 'c3', width: 100 } 
      ], 
     //Subgrid2...  
      subGrid: true, 
      subGridRowExpanded: function (subgrid2DivId, subRowId2) { 
       var subgrid2TableId = subgrid2DivId + "_t"; 
       $("#" + subgrid2DivId).html("<table id='" + subgrid2DivId + "'></table>"); 
       $("#" + subgrid2TableId).jqGrid({ 
        datatype: 'local', 
        data: secondSubgrid[subRowId2], 
        colNames: ['Col 1', 'Col 2', 'Col 3'], 
        colModel: [ 
         { name: 'd1', width: 100 }, 
         { name: 'd2', width: 100 }, 
         { name: 'd3', width: 100 } 
        ], 
       }); 

      }     
     }); 
    } 
}); 

有沒有想法嗎?

回答

8

我覺得你的問題有意思。我認爲答案可能對許多其他人有幫助。所以我寫了兩個演示,演示如何實現這些需求。

enter image description here

The first demo是從my previous answer哪些代碼您使用你的問題的文本已經使用的演示(這是基於在another one)基地。我還添加了the answer的技巧,以隱藏沒有子網格的行的子網格圖標(「+」)。

爲了簡化子網格數據的保持我在每一行subgrid屬性哪個值都爲次網格的數據添加。持有數據的類型非常實用,因爲jqGrid保存行的全部項目,因此您不需要更多隱藏列。要訪問本地數據,我建議使用getLocalRow方法。見the answer例如用於附加信息或getLocalRow方法,該方法是非常簡單的the source code

第一演示代碼:

var myData = [ 
     // main grid data 
     { id: "m1", col1: "11", col2: "12", 
      subgrid: [ 
       // data for subgrid for the id=m1 
       { id: "s1a", c1: "aa", c2: "ab", c3: "ac", 
        subgrid: [ 
         // data for subgrid for the id=m1, subgridId=s1a 
         { id: "2s1a", d1: "2aa", d2: "2ab", d3: "2ac" }, 
         { id: "2s1b", d1: "2ba", d2: "2bb", d3: "2bc" }, 
         { id: "2s1c", d1: "2ca", d2: "2cb", d3: "2cc" } 
        ]}, 
       { id: "s1b", c1: "ba", c2: "bb", c3: "bc" }, 
       { id: "s1c", c1: "ca", c2: "cb", c3: "cc" } 
      ]}, 
     { id: "m2", col1: "21", col2: "22", 
      subgrid: [ 
       // data for subgrid for the id=m2 
       { id: "s2a", c1: "xx", c2: "xy", c3: "xz", 
        subgrid: [ 
         // data for subgrid for the id=m2, subgridId=s2a 
         { id: "2s2a", d1: "xx", d2: "xy", d3: "xz" } 
        ]} 
      ]}, 
     { id: "m3", col1: "31", col2: "32" } 
    ], 
    removeSubgridIcon = function() { 
     var $this = $(this); 
     $this.find(">tbody>tr.jqgrow>td.ui-sgcollapsed").filter(function() { 
      var rowData = $this.jqGrid("getLocalRow", 
        $(this).closest("tr.jqgrow").attr("id")); 
      return rowData.subgrid == null; 
     }).unbind("click").html(""); 
    }, 
    isHasSubrids = function (data) { 
     var l = data.length, i; 
     for (i = 0; i < l; i++) { 
      if (data[i].subgrid != null) { 
       return true; 
      } 
     } 
     return false; 
    }; 

$("#list").jqGrid({ 
    datatype: "local", 
    data: myData, 
    colNames: ["Column 1", "Column 2"], 
    colModel: [ 
     { name: "col1", width: 200 }, 
     { name: "col2", width: 200 } 
    ], 
    gridview: true, 
    rownumbers: true, 
    autoencode: true, 
    sortname: "col1", 
    sortorder: "desc", 
    height: "100%", 
    pager: "#pager", 
    caption: "Demonstrate how to create subgrid from local hierarchical data", 
    subGrid: isHasSubrids(myData), 
    loadComplete: function() { 
     removeSubgridIcon.call(this); 
    }, 
    subGridRowExpanded: function (subgridDivId1, rowId1) { 
     var $subgrid1 = $("<table id='" + subgridDivId1 + "_t'></table>"), 
      localRowData1 = $(this).jqGrid("getLocalRow", rowId1); 
     $subgrid1.appendTo("#" + $.jgrid.jqID(subgridDivId1)); 
     $subgrid1.jqGrid({ 
      datatype: "local", 
      data: localRowData1.subgrid, 
      colNames: ["Colunm1", "Colunm2", "Colunm3"], 
      colModel: [ 
       { name: "c1", width: 112 }, 
       { name: "c2", width: 112 }, 
       { name: "c3", width: 112 } 
      ], 
      gridview: true, 
      rownumbers: true, 
      autoencode: true, 
      sortname: "c1", 
      sortorder: "desc", 
      height: "100%", 
      loadComplete: removeSubgridIcon, 
      subGrid: isHasSubrids(localRowData1.subgrid), 
      subGridRowExpanded: function (subgridDivId2, rowId2) { 
       var $subgrid2 = $("<table id='" + subgridDivId2 + "_t'></table>"), 
        localRowData2 = $(this).jqGrid("getLocalRow", rowId2); 
       $subgrid2.appendTo("#" + $.jgrid.jqID(subgridDivId2)); 
       $subgrid2.jqGrid({ 
        datatype: "local", 
        data: localRowData2.subgrid, 
        colNames: ["Col 1", "Col 2", "Col 3"], 
        colModel: [ 
         { name: "d1", width: 90 }, 
         { name: "d2", width: 90 }, 
         { name: "d3", width: 90 } 
        ], 
        gridview: true, 
        rownumbers: true, 
        autoencode: true, 
        sortname: "d1", 
        sortorder: "desc", 
        height: "100%", 
        subGrid: isHasSubrids(localRowData2.subgrid), 
        loadComplete: removeSubgridIcon 
       }); 
      } 
     }); 
    } 
}); 

The second demo是以前的演示更加深了修改。它可以用於創建非常深的多級子網格。我在演示中額外使用了密集的idPrefix來簡化使用ID的工作。我建議比較myData陣列中id屬性在第一個演示和第二個演示中的值。第二個演示代碼如下

var myData = [ 
     // main grid data 
     { id: "1", col1: "11", col2: "12", 
      subgrid: [ 
       // data for subgrid for the id=m1 
       { id: "1", c1: "aa", c2: "ab", c3: "ac", 
        subgrid: [ 
         // data for subgrid for the id=m1, subgridId=s1a 
         { id: "1", d1: "2aa", d2: "2ab", d3: "2ac" }, 
         { id: "2", d1: "2ba", d2: "2bb", d3: "2bc" }, 
         { id: "3", d1: "2ca", d2: "2cb", d3: "2cc" } 
        ]}, 
       { id: "2", c1: "ba", c2: "bb", c3: "bc" }, 
       { id: "3", c1: "ca", c2: "cb", c3: "cc" } 
      ]}, 
     { id: "2", col1: "21", col2: "22", 
      subgrid: [ 
       // data for subgrid for the id=m2 
       { id: "1", c1: "1xx", c2: "1xy", c3: "1xz", 
        subgrid: [ 
         // data for subgrid for the id=m2, subgridId=s2a 
         { id: "1", d1: "2xx", d2: "2xy", d3: "2xz" } 
        ]} 
      ]}, 
     { id: "3", col1: "31", col2: "32" } 
    ], 
    removeSubgridIcon = function() { 
     var $this = $(this), 
      idPrefix = $this.jqGrid("getGridParam", "idPrefix"); 
     $this.find(">tbody>tr.jqgrow>td.ui-sgcollapsed").filter(function() { 
      var rowData = $this.jqGrid("getLocalRow", 
        $.jgrid.stripPref(idPrefix, $(this).closest("tr.jqgrow").attr("id"))); 
      return rowData.subgrid == null; 
     }).unbind("click").html(""); 
    }, 
    isHasSubrids = function (data) { 
     var l = data.length, i; 
     for (i = 0; i < l; i++) { 
      if (data[i].subgrid != null) { 
       return true; 
      } 
     } 
     return false; 
    }, 
    specificGridOptions = [ 
     { 
      colNames: ["Column 1", "Column 2"], 
      colModel: [ 
       { name: "col1" }, 
       { name: "col2" } 
      ], 
      cmTemplate: { width: 200 }, 
      sortname: "col1", 
      sortorder: "desc", 
      idPrefix: "s_", 
      pager: "#pager", 
      caption: "Demonstrate how to create subgrid from local hierarchical data" 
     }, 
     { 
      colNames: ["Colunm1", "Colunm2", "Colunm3"], 
      colModel: [ 
       { name: "c1" }, 
       { name: "c2" }, 
       { name: "c3" } 
      ], 
      cmTemplate: { width: 112 }, 
      sortname: "c1", 
      sortorder: "desc" 
     }, 
     { 
      colNames: ["Col 1", "Col 2", "Col 3"], 
      colModel: [ 
       { name: "d1" }, 
       { name: "d2" }, 
       { name: "d3" } 
      ], 
      cmTemplate: { width: 90 }, 
      sortname: "d1", 
      sortorder: "desc" 
     } 
    ], 
    commonGridOptions = { 
     datatype: "local", 
     gridview: true, 
     rownumbers: true, 
     autoencode: true, 
     height: "100%", 
     loadComplete: function() { 
      // one can use loadComplete: removeSubgridIcon, but I included 
      // curent implementation of loadComplete only to show how to call 
      // removeSubgridIcon from your loadComplete callback handler 
      removeSubgridIcon.call(this); 
     }, 
     subGridRowExpanded: function (subgridDivId, rowId) { 
      var $subgrid = $("<table id='" + subgridDivId + "_t'></table>"), 
       subgridLevel = $(this).jqGrid("getGridParam", "subgridLevel") + 1, 
       parentIdPrefix = $(this).jqGrid("getGridParam", "idPrefix"), 
       pureRowId = $.jgrid.stripPref(parentIdPrefix, rowId), 
       localRowData = $(this).jqGrid("getLocalRow", pureRowId); 
      $subgrid.appendTo("#" + $.jgrid.jqID(subgridDivId)); 
      $subgrid.jqGrid($.extend(true, {}, commonGridOptions, specificGridOptions[subgridLevel], { 
       data: localRowData.subgrid, 
       subGrid: isHasSubrids(localRowData.subgrid), 
       subgridLevel: subgridLevel, 
       idPrefix: rowId + "_", 
       rowNum: 10000 // we use this to have no pager in the subgrids 
      })); 
     } 
    }; 

$("#list").jqGrid($.extend(true, {}, commonGridOptions, specificGridOptions[0], { 
    data: myData, 
    subgridLevel: 0, 
    subGrid: isHasSubrids(myData) 
})); 
+2

感謝Oleg,很棒的解決方案!直到看到這個,我開始失去希望。這正是我正在尋找的。保持良好的工作。 (我試圖投票作爲答案,但我沒有足夠的聲望點。) –

+0

@JonL:不客氣!這不是一個問題。如果你[[接受]](http://meta.stackexchange.com/a/5235/147495)我的回答,你會得到3個額外的聲望點。獲得15個聲望點後,您將有權每天投票30個問題或答案**。你第一個問題非常有趣。可能你會像我一樣喜歡stakoverflow。最好的祝願! – Oleg

+0

我們如何可以編輯的第一張demo的所有單元格@oleg – neverwinter