【问题标题】:jqGrid : issue loading nested sub grid with local datatypejqGrid:问题加载具有本地数据类型的嵌套子网格
【发布时间】:2012-11-25 21:16:29
【问题描述】:

我正在尝试让嵌套子网格使用 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 }
                    ],
                });

            }                
        });
    }
});

有什么想法吗?

【问题讨论】:

    标签: jquery jqgrid nested subgrid


    【解决方案1】:

    我觉得你的问题很有趣。我认为答案可能对其他人有帮助。所以我写了两个演示来演示如何实现需求。

    The first demo 基于来自my previous answer 的演示(基于another one),您使用的代码已在问题文本中使用。我还添加了来自the answer 的技巧,以隐藏没有子网格的行的子网格图标(“+”)。

    为了简化子网格数据的保存,我在每一行 subgrid 属性中添加了哪个值是子网格的数据。保存数据的方式非常实用,因为 jqGrid 保存了行的全部项目,因此您不再需要任何隐藏列。要访问本地数据,我建议使用getLocalRow 方法。更多信息请参见the answer 或getLocalRow 方法的the source code,非常简单。

    第一个demo的代码:

    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是对之前demo的更深修改。它可以用于创建非常深的多级子网格。我在演示中使用了额外密集的idPrefix 来简化使用ID。我建议在第一个演示和第二个演示中比较 myData 数组项的 id 属性值。第二个demo的代码如下

    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)
    }));
    

    【讨论】:

    • 感谢奥列格,很好的解决方案!在看到这个之前,我开始失去希望。这正是我正在寻找的。保持良好的工作。 (我试图投票作为答案,但我没有足够的声望点..)
    • @JonL:不客气!这不是一个问题。如果您"accept"我的回答,您将获得 3 个额外的声望点。在您获得 15 点声望点后,您将有权每天对 30 个问题或答案进行投票。你的第一个问题很有趣。可能你会像我一样喜欢 stakoverflow。最美好的祝愿!
    • @neverwinter:这取决于您要使用的编辑模式以及保存修改的位置。如果在单元格编辑模式下进行本地编辑,您只需将cellEdit: true, cellsubmit: "clientArray" 选项添加到所有需要可编辑的网格/子网格。
    • @Oleg 我的数据在 [{id:1,code:"112",name:"aa","group":"1"},{id:2,code:"112 ",name:"aa","group":"2"},{id:3,code:"113",name:"bb","group":"1"},{id:4,code: "113",name:"bb","group":"2"}] 我把它转换成这种格式 data["112"] = {code:"112",name:"aa",subgrid[{grp1obj ,grp2obj}]}, data["113"] = {code:"113",name:"bb",subgrid[{grp1obj,grp2obj}]},现在我无法将数据分配给主网格。
    • @Haider:对不起,但我不明白你在做什么以及你试图实现什么。您使用(或可以使用)jqGrid 的哪个版本和哪个分支?您使用datatype: "local" 还是从服务器加载数据?你用什么colModel?您应该打开新问题并详细描述所有内容。
    猜你喜欢
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多