2012-04-15 57 views
8

我想讓我的子網格與本地數據一起工作。然而,當我點擊展開時,我只是得到一個裝載框,就像網格試圖從某處拉取數據。由於主網格的數據類型爲datatype:'local',因此我假設我不需要subGridUrl。還有什麼我應該做的?jqGrid子網格與「本地」數據

回答

21

沒有直接的方法可以用本地數據定義子網格,但使用subGridRowExpandedSubgrid as Grid)可以相對容易地實現相同的行爲。人們需要做的只是從網格的rowid中獲取一些內部結構的子網格數據。例如,如果你將有局部柵格地圖作爲

var myGridData = [ 
     // main grid data 
     {id: "m1", col1: "11", col2: "12"}, 
     {id: "m2", col1: "21", col2: "22"} 
    ], 
    mySubgrids = { 
     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"} 
     ] 
    }; 

裏面的subGridRowExpanded你可以用下面的代碼創建子網格:

$("#grid").jqGrid({ 
    datatype: 'local', 
    data: myGridData, 
    colNames: ['Column 1', 'Column 2'], 
    colModel: [ 
     { name: 'col1', width: 200 }, 
     { name: 'col2', width: 200 } 
    ], 
    ... 
    subGrid: true, 
    subGridRowExpanded: function (subgridDivId, rowId) { 
     var subgridTableId = subgridDivId + "_t"; 
     $("#" + subgridDivId).html("<table id='" + subgridTableId + "'></table>"); 
     $("#" + subgridTableId).jqGrid({ 
      datatype: 'local', 
      data: mySubgrids[rowId], 
      colNames: ['Col 1', 'Col 2', 'Col 3'], 
      colModel: [ 
       { name: 'c1', width: 100 }, 
       { name: 'c2', width: 100 }, 
       { name: 'c3', width: 100 } 
      ], 
      ... 
     }); 
    } 
}); 

The demo顯示結果住:

enter image description here

+0

大回答奧列格。我將在我的代碼中實現這個功能!我實際上使用這個結合從網格到網格的拖放(從我的其他[問題](http://stackoverflow.com/questions/10146892/jqgrid-drag-and-drop-row-check)) 。 – FastTrack 2012-04-16 20:24:42

+0

@FastTrack:不客氣! – Oleg 2012-04-16 20:25:59

+0

@oleg很好的回答,它幫助我解決了一個重大問題,儘管我只能對它進行一次提升。 :( – 2012-06-07 07:24:28