支持jqGrid中未知数量的层次结构

时间:2011-12-18 14:12:43

标签: javascript jquery jqgrid tree

我正在使用jqGrid,我需要显示一个具有未知级别(子网格)的树网格。

在jqGrid演示中有一个分层网格的例子,但只有事先知道级别的数量才有好处,在这种情况下它只支持2个级别。

以下是代码:jqGrid subgrid example

知道如何提前支持未知数量的树节点吗?即,有时它可能是2级,有时是1级(只有根级),有时是4级等等......

谢谢

1 个答案:

答案 0 :(得分:0)

我这样做。

$("#TopLevelGrid").jqGrid({
    ...        
    subGrid: true,
    subGridRowExpanded: expandScript,
    ...
});
// Append information about the level to the TopLevelGrid html-table element
$("#TopLevelGrid").data("currentLevelId", 1);
$("#TopLevelGrid").addClass("MyGrid");      // Ensure that html-table has class MyGrid

function expandScript(parentDivId, parentRowId)
{
        ...
        var parentTable = $("#" + parentDivId").closest("table.MyGrid");
        var parentLevelId = $(parentTable).data("currentLevelId");

        var currentLevelId = parentLevelId + 1;                     // calculate the ID of the current level
        var currentLevelHasChildLevel = true;                       // calculate if the current level has a childLevel
        // Append a table-element which will hold the new grid;     // the same as the jqGrid-subgrid example
        $('#' + parentDivId).append('<table class=\"MyGrid\" id=\"ChildGrid_' + currentLevelId + '\"></table><div id=\"Pager_ChildGrid_' + currentLevelId + '\"></div><br />');
        $('#ChildGrid_' + currentLevelId ').jqGrid({
            ...
            subGrid: currentLevelHasChildLevel,
            subGridRowExpanded: expandScript,                       // recursive call to expandScript function
            ...
        });
        // Save the currentLevelId as custom-data element bound to the html-table element which will also hold the child-grid
        $("#ChildGrid_" + currentLevelId).data("currentlevelid", currentLevelId);
        ...
}