0

メイン グリッドからサブグリッド データを読み込もうとしています。メイン グリッドは正常に読み込まれますが、行をクリックするとサブグリッド データが読み込まれません。何が欠けているのかわからない。助けてください。

以下は私が使用しているコードです。

<script type="text/javascript">
//<![CDATA[
$(document).ready(function () {
    var mydata = [],
        grid = $("#list");
    var mainGridPrefix = "s_";

    grid.jqGrid({
        url: '${recordsUrl}',
        datatype: 'json',
        ignoreCase: true,
        mtype: 'GET',
        colNames: ['Global Search', 'serverId', 'DeviceName', 'Description', 'Console', 'OS', 'Business Unit', 'Model', 'Manufacturer', 'Serial Number', 'LifeCycle', 'Tier'],
        colModel: [{
            name: 'globalSearch',
            index: 'globalSearch',
            width: 50,
            hidden: true,
            searchoptions: {
                searchhidden: true
            }
        }, {
            name: 'serverId',
            index: 'serverId',
            width: 10,
            hidden: true
        }, {
            name: 'deviceName',
            index: 'deviceName',
            width: 50
        }, {
            name: 'serverDesc',
            index: 'serverDesc',
            width: 70
        }, {
            name: 'console',
            index: 'console',
            width: 50
        }, {
            name: 'groupName',
            index: 'groupName',
            width: 40
        }, {
            name: 'businessUnit',
            index: 'businessUnit',
            width: 30
        }, {
            name: 'model',
            index: 'model',
            width: 30,
            editable: true,
            editrules: {
                required: true,
                edithidden: true
            },
            hidden: true,
            editoptions: {
                dataInit: function (element) {
                    $(element).attr("readonly", "readonly");
                }
            }
        }, {
            name: 'manufacturer',
            index: 'manufacturer',
            width: 30,
            editable: true,
            editrules: {
                required: true,
                edithidden: true
            },
            hidden: true,
            editoptions: {
                dataInit: function (element) {
                    $(element).attr("readonly", "readonly");
                }
            }
        }, {
            name: 'serialNumber',
            index: 'serialNumber',
            width: 30,
            editable: true,
            editrules: {
                required: true,
                edithidden: true
            },
            hidden: true,
            editoptions: {
                dataInit: function (element) {
                    $(element).attr("readonly", "readonly");
                }
            }
        }, {
            name: 'lifeCycle',
            index: 'lifeCycle',
            width: 30,
            editable: true,
            editrules: {
                required: true,
                edithidden: true
            },
            hidden: true,
            editoptions: {
                dataInit: function (element) {
                    $(element).attr("readonly", "readonly");
                }
            }
        }, {
            name: 'tier',
            index: 'tier',
            width: 30,
            editable: true,
            editrules: {
                required: true,
                edithidden: true
            },
            hidden: true,
            editoptions: {
                dataInit: function (element) {
                    $(element).attr("readonly", "readonly");
                }
            }
        }],
        postData: {},
        rowNum: 10,
        rowList: [10, 20, 40, 60],
        height: 'auto',
        autowidth: true,
        rownumbers: true,
        pager: '#pager',
        sortname: 'deviceName',
        viewrecords: true,
        sortorder: "asc",
        caption: "Records",
        emptyrecords: "Empty records",
        idPrefix: mainGridPrefix,
        loadonce: true,
        loadComplete: function () {},
        jsonReader: {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: false,
            cell: "cell",
            id: "serverId"
        },
        subGrid: true,
        beforeProcessing: function (data) {
            var rows = data.rows,
                l = rows.length,
                i, item, subgrids = {};
            for (i = 0; i < l; i++) {
                item = rows[i];
                if (item.apps) {
                    subgrids[item.id] = item.apps;
                }
            }
            data.userdata = subgrids;
        },
        subGridRowExpanded: function (subgridDivId, rowId) {
            var $subgrid = $("<table id='" + subgridDivId + "_t'></table>"),
                pureRowId = $.jgrid.stripPref(mainGridPrefix, rowId),
                subgrids = $(this).jqGrid("getGridParam", "userData");

            $subgrid.appendTo("#" + $.jgrid.jqID(subgridDivId));
            $subgrid.jqGrid({
                datatype: "local",
                data: subgrids[pureRowId],
                colNames: ["App Id", "Desc"],
                colModel: [{
                    name: "appId"
                }, {
                    name: "applicationDesc"
                }],
                sortname: "applicationDesc",
                height: "100%",
                rowNum: 10000,
                autoencode: true,
                autowidth: true,
                jsonReader: {
                    repeatitems: false,
                    id: "appId"
                },
                gridview: true,
                idPrefix: rowId + "_"
            });
        }

    });


    $("#search").click(function () {
        var searchFiler = $("#filter").val(),
            f;

        if (searchFiler.length === 0) {
            grid[0].p.search = false;
            $.extend(grid[0].p.postData, {
                filters: ""
            });
        }
        f = {
            groupOp: "OR",
            rules: []
        };
        f.rules.push({
            field: "globalSearch",
            op: "cn",
            data: searchFiler
        });
        f.rules.push({
            field: "deviceName",
            op: "cn",
            data: searchFiler
        });
        grid[0].p.search = true;
        $.extend(grid[0].p.postData, {
            filters: JSON.stringify(f)
        });
        grid.trigger("reloadGrid", [{
            page: 1,
            current: true
        }]);
    });

    grid.jqGrid('navGrid', '#pager', {
        edit: false,
        add: false,
        del: false,
        search: true,
        view: true
    }, {}, {}, {}, { // search
        sopt: ['cn', 'eq', 'ne', 'lt', 'gt', 'bw', 'ew'],
        closeOnEscape: true,
        multipleSearch: true,
        closeAfterSearch: true
    }, { // vew options
        beforeShowForm: function (form) {
            $("tr#trv_id", form[0]).show();
        },
        afterclickPgButtons: function (whichbutton, form, rowid) {
            $("tr#trv_id", form[0]).show();
        }
    });

    grid.navButtonAdd('#pager', {
        caption: "Add",
        buttonicon: "ui-icon-plus",
        onClickButton: addRow,
        position: "last",
        title: "",
        cursor: "pointer"
    });

    grid.navButtonAdd('#pager', {
        caption: "Edit",
        buttonicon: "ui-icon-pencil",
        onClickButton: editRow,
        position: "last",
        title: "",
        cursor: "pointer"
    });

    grid.navButtonAdd('#pager', {
        caption: "Delete",
        buttonicon: "ui-icon-trash",
        onClickButton: deleteRow,
        position: "last",
        title: "",
        cursor: "pointer"
    });
});
//]]>

4

1 に答える 1

0

サブグリッドの colModel に index: 情報を追加するのを忘れていました。名前が大文字の場合は、大文字の使用にも注意する必要があります。その場合は、インデックスも大文字にする必要があります。そうすれば、jqgrid はデータを配置する必要がある場所を認識できます。これは修正されたコードであり、実際の例へのリンクでもあります

ここに画像の説明を入力

//<![CDATA[
$(document).ready(function () {
var mydata = [{
    globalsearch: "1",
    serverId: "Test Name1",
    deviceName: "Test Address1",
    Console: "Test Date1"
}, {
    globalsearch: "2",
    serverId: "Test Name2",
    deviceName: "Test Address2",
    Console: "Test Date2"
}, {
    globalsearch: "3",
    serverId: "blah",
    deviceName: "hello",
    Console: "basketball"
}];
grid = $("#list");


var mydata2 = [{
    AppId: "1",
    Desc: "Test Name1"
}, {
    AppId: "2",
    Desc: "Test Name2"
}, {
    AppId: "3",
    Desc: "blah"
}];
var mainGridPrefix = "s_";

grid.jqGrid({
    datatype: 'local',
    data: mydata,
    colNames: ['globalsearch', 'serverId', 'deviceName', 'Description', 'Console', 'OS', 'Business Unit', 'Model', 'Manufacturer', 'Serial Number', 'LifeCycle', 'Tier'],
    colModel: [{
        name: 'globalsearch',
        index: 'globalsearch',
        width: 50,
    }, {
        name: 'serverId',
        index: 'serverId',
        width: 10,
        hidden: false
    }, {
        name: 'deviceName',
        index: 'deviceName',
        width: 50
    }, {
        name: 'serverDesc',
        index: 'serverDesc',
        width: 70
    }, {
        name: 'Console',
        index: 'Console',
        width: 50
    }, {
        name: 'groupName',
        index: 'groupName',
        width: 40
    }, {
        name: 'businessUnit',
        index: 'businessUnit',
        width: 30
    }, {
        name: 'model',
        index: 'model',
        width: 30,
        editable: true,
        editrules: {
            required: true,
            edithidden: true
        },
        hidden: true,
        editoptions: {
            dataInit: function (element) {
                $(element).attr("readonly", "readonly");
            }
        }
    }, {
        name: 'manufacturer',
        index: 'manufacturer',
        width: 30,
        editable: true,
        editrules: {
            required: true,
            edithidden: true
        },
        hidden: true,
        editoptions: {
            dataInit: function (element) {
                $(element).attr("readonly", "readonly");
            }
        }
    }, {
        name: 'serialNumber',
        index: 'serialNumber',
        width: 30,
        editable: true,
        editrules: {
            required: true,
            edithidden: true
        },
        hidden: true,
        editoptions: {
            dataInit: function (element) {
                $(element).attr("readonly", "readonly");
            }
        }
    }, {
        name: 'lifeCycle',
        index: 'lifeCycle',
        width: 30,
        editable: true,
        editrules: {
            required: true,
            edithidden: true
        },
        hidden: true,
        editoptions: {
            dataInit: function (element) {
                $(element).attr("readonly", "readonly");
            }
        }
    }, {
        name: 'tier',
        index: 'tier',
        width: 30,
        editable: true,
        editrules: {
            required: true,
            edithidden: true
        },
        hidden: true,
        editoptions: {
            dataInit: function (element) {
                $(element).attr("readonly", "readonly");
            }
        }
    }],
    rowNum: 10,
    rowList: [10, 20, 40, 60],
    rownumbers: true,
    pager: '#pager',
    sortname: 'deviceName',
    viewrecords: true,
    sortorder: "asc",
    caption: "Records",
    height: 'auto',
    autowidth: true,
    loadComplete: function () {},
    subGrid: true,
    beforeProcessing: function (data) {
        var rows = data.rows,
            l = rows.length,
            i, item, subgrids = {};
        for (i = 0; i < l; i++) {
            item = rows[i];
            if (item.apps) {
                subgrids[item.id] = item.apps;
            }
        }
        data.userdata = subgrids;
    },
    subGridRowExpanded: function (subgridDivId, rowId) {
        var $subgrid = $("<table id='" + subgridDivId + "_t'></table>"),
            pureRowId = $.jgrid.stripPref(mainGridPrefix, rowId),
            subgrids = $(this).jqGrid("getGridParam", "userData");

        $subgrid.appendTo("#" + $.jgrid.jqID(subgridDivId));
        $subgrid.jqGrid({
            datatype: "local",
            data: mydata2,
            colNames: ["AppId", "Desc"],
            colModel: [{
                name: "AppId",
                index: "AppId"
            }, {
                name: "Desc",
                index: "Desc"
            }],
            sortname: "applicationDesc",
            height: "100%",
            rowNum: 10000,
            autoencode: true,
            autowidth: true,
            jsonReader: {
                repeatitems: false,
                id: "appId"
            },
            gridview: true,
            idPrefix: rowId + "_"
        });
    }

});


$("#search").click(function () {
    var searchFiler = $("#filter").val(),
        f;

    if (searchFiler.length === 0) {
        grid[0].p.search = false;
        $.extend(grid[0].p.postData, {
            filters: ""
        });
    }
    f = {
        groupOp: "OR",
        rules: []
    };
    f.rules.push({
        field: "globalSearch",
        op: "cn",
        data: searchFiler
    });
    f.rules.push({
        field: "deviceName",
        op: "cn",
        data: searchFiler
    });
    grid[0].p.search = true;
    $.extend(grid[0].p.postData, {
        filters: JSON.stringify(f)
    });
    grid.trigger("reloadGrid", [{
        page: 1,
        current: true
    }]);
});

grid.jqGrid('navGrid', '#pager', {
    edit: false,
    add: false,
    del: false,
    search: true,
    view: true
}, {}, {}, {}, { // search
    sopt: ['cn', 'eq', 'ne', 'lt', 'gt', 'bw', 'ew'],
    closeOnEscape: true,
    multipleSearch: true,
    closeAfterSearch: true
}, { // vew options
    beforeShowForm: function (form) {
        $("tr#trv_id", form[0]).show();
    },
    afterclickPgButtons: function (whichbutton, form, rowid) {
        $("tr#trv_id", form[0]).show();
    }
});

grid.navButtonAdd('#pager', {
    caption: "Add",
    buttonicon: "ui-icon-plus",
    onClickButton: addRow,
    position: "last",
    title: "",
    cursor: "pointer"
});

grid.navButtonAdd('#pager', {
    caption: "Edit",
    buttonicon: "ui-icon-pencil",
    onClickButton: editRow,
    position: "last",
    title: "",
    cursor: "pointer"
});

grid.navButtonAdd('#pager', {
    caption: "Delete",
    buttonicon: "ui-icon-trash",
    onClickButton: deleteRow,
    position: "last",
    title: "",
    cursor: "pointer"
});
});
//]]>



<table id="list"></table>
<div id="pager"></div>
于 2013-03-01T16:59:12.840 に答える