【问题标题】:How to show total of subgrid footer data in parent jqGrid footer如何在父 jqGrid 页脚中显示子网格页脚数据的总数
【发布时间】:2013-11-03 01:43:41
【问题描述】:

我需要带有子网格的 jqGrid,我将从单个存储过程中加载所有 jqGrid 数据(父网格和子网格)。其中我的 SP 正在向我返回包含以下列的表格。

Login | ReadingID | Role | Update Time | Comments

我想在父 jqGrid 中显示登录名。这将是父网格中的唯一列。 & 对于每条记录,我需要有子网格显示与该登录相对应的阅读 ID 记录。对于任何登录,阅读 ID 可能有重复的条目。 Login 和 ReadingID 之间存在一对多的基数。现在我想在 subGrid 页脚中为每个登录显示不同的阅读 ID 计数和更新时间计数。 & 此外,我希望在父网格的页脚中包含所有这些子网格页脚总数。

我希望要求足够明确。有没有人实现过这样的 jqGrid 或创建了演示。

更新以描述需求和选择的解决方案

我使用网格定义如下

mygrid = $("#RptDocRelease");

    // create the grid without filling it (datatype: "local")
    mygrid.jqGrid({
        datatype: 'local',// to revent initial loading of the grid
        postData: {
            FromDate: function () { return $("#FromDate").val(); },
            ToDate: function () { return $("#ToDate").val(); }
        },
        url: '@Url.Action("DocReleaseProductivityList", "Reports")',
        jsonReader: { repeatitems: false, root: "DocRows" },
        colNames: ['@VirtuOxAdmin.DocReleaseProductivity_Grid_RptDocRelease_Login'],
        colModel: [{ name: 'Login', index: 'Login', }],
        idPrefix: mainGridPrefix,
        subGrid: true,
        gridview: true,
        viewrecords: true,
        pgbuttons: false, pginput: false,
        recordtext: 'Total Rows:{2}',
        emptyrecords: 'Total Rows:0',
        pager: '#LogPager',
        caption: '@VirtuOxAdmin.DocReleaseProductivity_Grid_RptDocRelease_Title',
        height: 'auto',
        width: 770,
        userDataOnFooter: true,
        hidegrid: false,
        headertitles: true,
        loadError: function (xhr, status, error) {
            alert(status + " " + error);
        },
        beforeProcessing: function (data) {
            var rows = data.DocRows, l = rows.length, i, item, subgrids = {};
            for (i = 0; i < l; i++) {
                item = rows[i];
                if (item.id) {
                    subgrids[item.id] = item.ReadingList;
                }
            }
            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],
                jsonReader: { repeatitems: false },
                colNames: ['@VirtuOxAdmin.DocReleaseProductivity_Grid_RptDocRelease_ReadingID',
                            '@VirtuOxAdmin.DocReleaseProductivity_Grid_RptDocRelease_Role',
                            '@VirtuOxAdmin.DocReleaseProductivity_Grid_RptDocRelease_UpdateTime',
                            '@VirtuOxAdmin.DocReleaseProductivity_Grid_RptDocRelease_Comments'
                ],
                colModel: [
                    { name: 'ReadingID', index: 'ReadingID', width: 80, fixed: true, sorttype: "integer" },
                    { name: 'Role', index: 'Role', width: 100, fixed: true },
                    {
                        name: 'UpdateTime', index: 'UpdateTime', width: 80, fixed: true, sorttype: "date", formatter: "date",
                        formatoptions: { srcformat: "m/d/Y h:i:s", newformat: "m/d/Y h:i:s A" }
                    },
                    { name: 'Comments', index: 'Comments' }
                ],
                cmTemplate: { align: 'center', sorttable: false, cellattr: function () { return 'style="white-space: normal!important;' } },
                height: 'auto',
                width: '100%',
                hidegrid: false,
                viewrecords: true,
                pgbuttons: false, pginput: false,
                recordtext: 'Total Rows:{2}',
                emptyrecords: 'Total Rows:0',
                pager: rowId + '_Pager',
                userDataOnFooter: true,
                headertitles: true,
                gridview: true,
                loadError: function (xhr, status, error) {
                    alert(status + " " + error);
                },
                gridview: true,
                idPrefix: rowId + "_"
            });
            $("#" + rowId + "_Pager").navGrid('#LogPager', { edit: false, add: false, del: false, search: false, refresh: false })
        }
    });
    mygrid.navGrid('#LogPager', { edit: false, add: false, del: false, search: false, refresh: false })
    .closest(".ui-jqgrid").hide();

&服务器数据将按以下结构返回

 public struct JQGridResult
    {
        public int page;
        public int total;
        public int records;
        public List<ReportData> DataRows;
        public List<DocRelease> DocRows;
        public object userdata;
    }


 public struct DocRelease
    {
        public string Login { set; get; }
        public List<Readings> ReadingList { set; get; }
        public object userdata;
    }

public struct Readings
    {
        public int ReadingID { set; get; }
        public string Role { set; get; }
        public DateTime UpdateTime { set; get; }
        public string Comments { set; get; }
    }

【问题讨论】:

    标签: jquery jqgrid


    【解决方案1】:

    似乎我最容易使用userdata 以及主网格和子网格的userDataOnFooter: true 选项(请参阅the old answer)。在这种情况下,您可以在服务器端对汇总行值进行所有必需的计算,并将计算结果包含在userdata 部分中。 The answerthis onethis one 和许多其他提供了同时加载整个网格和子网格的示例。您可以使用loadonce: true 来简化加载。我认为您将能够根据参考的答案实现您的要求。不要忘记在子网格中使用idPrefix 选项。

    更新:在您发布带有数据的图片后,我认为您应该使用分组而不是子网格。您仍然可以使用footerrow: trueuserDataOnFooter: true 选项来设置总页脚行。我刚刚为the answer 创建了the demo,并添加了选项footerrowuserDataOnFooteruserData。结果我们得到the demo,它显示了与您需要的结构相同的数据:

    【讨论】:

    • 我知道我应该如何定义网格和子网格以通过单个服务器调用加载它以及从服务器检索哪些数据。但是关于主网格的页脚数据存在一个混淆。所有子网格页脚计数的总和 [可能是阅读 ID 的不同计数或只是更新时间的计数] 是否会显示在最后一个子网格页脚中显示的计数下方。我的意思是说,主网格上的最终计数应该以这样的方式显示,它应该沿着子网格的阅读 ID 和更新时间列。
    • @Shaggy:您可以完全控制在任何子网格或主网格中使用页脚。我希望你使用subgrid as grid。在这种情况下,子网格只是一个具有所有选项的网格,如标准网格。所以你可以实现所有你想要的。如果您在子网格中使用userDataOnFooter: true 并在输入数据中使用userdata,那么您将在子网格中有汇总行。您还可以在主网格中使用页脚。所以我可以重复一遍,你可以实现所有你想要的。
    • 感谢奥列格的帮助。而不是在执行此要求之前清除我的所有疑问;我想我应该开始实施它,然后随着它的进一步发展,如果我遇到任何问题,我会与您保持联系。非常感谢!!
    • 我正在根据您建议的第一个答案here 定义我的网格。但是我在 beforeProcessing 事件中感到困惑,您正在使用子网格 ID 设置用户数据。现在我的问题是我将从服务器端发送用户数据,以便根据我的要求在主网格页脚中显示总计。所以我想知道我应该如何处理这种情况。
    • @Shaggy:不客气!您可以提出新问题,详细描述您遇到的问题。请尝试制定新问题,以便它最清楚并且可能有示例(演示)。目标是您的问题应该对其他访问者有一些价值。您应该考虑到其他用户找不到来自 cmets 的信息。
    猜你喜欢
    • 1970-01-01
    • 2013-03-19
    • 2015-03-13
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    相关资源
    最近更新 更多