10

我试图让我的子网格处理本地数据。但是,当我单击展开时,我只是得到一个加载框,就像网格试图从某个地方提取数据一样。我假设我不需要 asubGridUrl因为主网格的数据类型是datatype:'local'. 还有什么我应该做的吗?

4

1 回答 1

25

没有直接的方法来使用本地数据定义子网格,但是您可以使用subGridRowExpanded子网格作为网格)相对容易地实现相同的行为。需要做的只是通过网格的 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 }
            ],
            ...
        });
    }
});

该演示实时显示结果:

在此处输入图像描述

于 2012-04-16T17:01:46.900 回答