【问题标题】:JQGrid Expand/Collapse all very slowJQGrid 展开/折叠都很慢
【发布时间】:2016-10-24 19:39:51
【问题描述】:

我有一个 JQGrid,并且在网格上实现了一个 Collapse/Expand All 按钮。它有效,但速度非常慢。这是我使用的代码,仅 120 行需要 5-6 秒。有什么办法可以提高这个性能吗?提前致谢!

        function CollapseAll() {
            $(".ui-icon-circlesmall-minus").trigger("click");
            $("#grid_toppager_left").find('.ui-icon-minus').removeClass('ui-icon-minus').addClass("ui-icon-plus");
        }

        function ExpandAll() {
            $(".ui-icon-circlesmall-plus").trigger("click");
            $("#grid_toppager_left").find('.ui-icon-plus').removeClass('ui-icon-plus').addClass("ui-icon-minus");
        }

        var groups = [];

        $("#grid").jqGrid({
                    url: '@Url.Action("GetLoanReport", "Report")',
                    datatype: "json",
                    emptyrecords: "0 records found",
                    height: "auto",
                    mtype: 'POST',
                    maxHeight: maxHeight,
                    postData: { startDate: $("#startDate").val(), endDate: $("#endDate").val(), selectedStatuses: selectedStatuses, selectedProductGroups: selectedProductGroups, assignedBranchList: assignedBranchList, assignedToList: assignedToList, createdByList: createdByList, approvedByList: approvedByList, uploadedByList: uploadedByList },
                    colNames: ['Branch', 'Status', 'Employee', 'Application ID', 'Customer Name', 'CustNo', 'Product Type', 'Description', 'Security Code', 'Final Rate', 'New Money', 'Total'],
                    colModel: [
                      { name: 'Branch', index: 'Branch', cellattr: function () { return ' title="my custom fixed tooltip for the column"'; } },
                      { name: 'Status', index: 'Status' },
                      { name: 'EmplName', index: 'EmplName' },
                      { name: 'ApplicationID', index: 'ApplicationID', sorttype: 'number', width: 125, sortable: true, formatter: createLink },
                      { name: 'CustName', index: 'CustName', formatter: custnameFormatter, width: 200, sortable: true },
                      { name: 'CustNo', index: 'CustNo', hidden: true, sortable: true },
                      { name: 'ProductType', index: 'ProductType', width: 100, sortable: true, sorttype: "text" },
                      { name: 'ProdDesc', index: 'ProdDesc', width: 250, sortable: true },
                      { name: 'SecurityCode', index: 'SecurityCode', width: 125, sortable: true },
                      { name: 'FinalRate', index: 'FinalRate', width: 75, align: "right", formatter: 'currency', formatoptions: { suffix: '%' }, sorttype: 'currency', sortable: true },
                      { name: 'NewMoney', index: 'NewMoney', formatter: 'currency', align: "right", sorttype: 'currency', formatoptions: { thousandsSeparator: ",", decimalPlaces: 0, prefix: "$" }, width: 125, sortable: true },
                      { name: 'TotalNewMoney', index: 'TotalNewMoney', formatter: 'currency', align: "right", sorttype: 'currency', formatoptions: { thousandsSeparator: ",", decimalPlaces: 0, prefix: "$" }, width: 125, sortable: true }
                    ],
                    jsonReader: {
                        repeatitems: false,
                        root: 'rowdata',
                        page: 'currpage',
                        total: 'totalpages',
                        records: 'totalrecords'
                    },
                    loadComplete: function () {
                        WaitIndicatorClose();

                        var reportSum = $("#grid").jqGrid('getCol', 'TotalNewMoney', false, 'sum');

                        $("#gridPager_right").html("<div id='sumTotal'>Number of Applications: " + $("#grid").getGridParam("records") + ",  Total: $" + formatMoney(reportSum, false, null, null, null, true, false) + '</div>');
                        $("#gridPager_right").show();

                        if (firstLoad == true) {
                            $(".ui-icon-circlesmall-plus, .ui-icon-circlesmall-minus").each(function () {
                                groups.push({ hid: $(this).closest("tr").attr("id"), collapsed: true });
                            });
                        } else {
                            $("#grid tr").each(function () {
                                for (var i = 0; i < groups.length; i++) {
                                    if ($(this).attr("id") === groups[i].hid) {
                                        if (groups[i].collapsed == false) {
                                            $("#grid").jqGrid('groupingToggle', groups[i].hid);
                                        }
                                    }
                                }
                            });
                        }

                        firstLoad = false;

                        $(".gridghead_0").attr("title", "Created by Branch");

                        $(".appLink").on("click", function (e) {
                            var appID = e.currentTarget.innerHTML;

                            ConfirmBox("This will redirect you to the Application page.  Are you sure?",
                                    function () {
                                        $.ajaxSetup({ cache: false });
                                        $.ajax({
                                            cache: false,
                                            type: "Get",
                                            url: "@Url.Action("VerifyAndSetApplicationID", "Application")",
                                            data: { "applicationID": appID },
                                            success: function (data) {
                                                if (data.Error) {
                                                    MessageBox(data.Error);
                                                } else {
                                                    if (data.Success) {
                                                        InitializeWriteAccess(appID);
                                                    } else {
                                                        MessageBox(data.NotFound);
                                                    }
                                                }
                                            },
                                            error: function (xhr, status, error) {
                                            },
                                            complete: function () {
                                            }
                                        });
                                    },
                                    function () {
                                    });

                        });
                    },
                    loadError: function () {
                        WaitIndicatorClose();
                    },
                    loadui: 'disable',
                    grouping: true,
                    onClickGroup: function (hid, collapsed) {
                        if ($(".ui-icon-circlesmall-plus").length == 0) {
                            $("#grid_toppager_left").find('.ui-icon-plus').removeClass('ui-icon-plus').addClass("ui-icon-minus");
                        } else {
                            $("#grid_toppager_left").find('.ui-icon-minus').removeClass('ui-icon-minus').addClass("ui-icon-plus");
                        }

                        for (var i = 0; i < groups.length; i++) {
                            if (groups[i].hid == hid) {
                                groups[i].collapsed = collapsed;
                            }
                        }

                        if (collapsed == true) {
                            $(".ui-icon-circlesmall-minus:hidden").each(function () {
                                for (var i = 0; i < groups.length; i++) {
                                    if (groups[i].hid == $(this).closest("tr").attr("id")) {
                                        groups[i].collapsed = true;
                                    }
                                }
                            });
                        }

                        $('#grid').trigger('reloadGrid');
                    },
                    groupingView: {
                        groupField: ['Branch', 'Status', 'EmplName'],
                        groupText: ['<b>{0}</b> Count: ({1})', '<b>{0}</b> Count: ({1})', '<b>Created by {0}</b> Count: ({1})'],
                        groupSummary: true,
                        groupColumnShow: false,
                        groupSummaryPos: "header",
                        groupCollapse: true
                    },
                    loadonce: true,
                    rowNum: 10000,
                    showrownumbers: true,
                    toppager: true,
                    shrinkToFit: true,
                    pgbuttons: false,
                    pginput: false,
                    pager: gridPager
                });
                $("#grid").jqGrid('navGrid', '#gridPager', { add: false, edit: false, del: false, find: false, search: false, refresh: false });
                $("#grid").jqGrid('navGrid', '#grid_toppager', { add: false, edit: false, del: false, find: false, search: false, refresh: false, width: 1093 });
                $("#grid").jqGrid('navButtonAdd', '#grid_toppager_left', {
                    caption: "Expand/Collapse All",
                    buttonicon: "ui-icon-plus",
                    onClickButton: function () {
                        if ($(".ui-icon-circlesmall-plus").length == 0) {
                            CollapseAll();
                        } else {
                            ExpandAll();
                        }

                        $('#grid').trigger('reloadGrid');
                    }
                });
                $("#grid").jqGrid('navButtonAdd', '#gridPager', {
                    caption: "Export to Excel",
                    onClickButton: function () {
                        fnExcelReport();
                    }
                });
            });

【问题讨论】:

  • 您使用(可以使用)哪个版本的 jqGrid 以及来自哪个 jqGrid 分支(free jqGrid、商业Guiddo jqGrid JS 或版本 here)。
  • 对不起,我用的是免费的 jqGrid
  • 哪个版本?重现问题的 JavaSctript 代码或演示在哪里?
  • 我相信它是 4.13.3 版本。我不确定是否可以发布演示,它是针对我为工作编写的一些银行软件。这是展开/折叠中涉及的所有代码......我只是根据它是加号还是减号触发每个图标上的 click() 。 .trigger() 调用需要时间

标签: jquery jqgrid


【解决方案1】:

我想您的代码中存在您未发布的问题。

我用 120 行虚拟数据和寻呼机中的“全部展开/折叠”按钮创建了 the demo。它使用free jqGrid 的当前 (4.13.4) 版本。在我的测试中,与之前 (4.13.3) 版本的免费 jqGrid 相同的演示的性能是相同的。大家可以试试看,性能不错。

我使用了“全部展开/折叠”按钮的一些修改代码,但是对于展开/折叠的性能应该没有区别。

.jqGrid("navButtonAdd", {
    caption: "Expand/Collapse All",
    id: "expand_collapse_all",
    buttonicon: "ui-icon-plus",
    onClickButton: function () {
        var gridId = this.id, icons = this.p.treeIcons,
            $expandCollapseAll = $("#expand_collapse_all");
        if ($expandCollapseAll.find('.ui-icon-minus').length > 0) {
            $expandCollapseAll.find('.ui-icon-minus')
                .removeClass('ui-icon-minus')
                .addClass("ui-icon-plus");
            $(".tree-minus").trigger("click");
        } else {
            $expandCollapseAll.find('.ui-icon-plus')
                .removeClass('ui-icon-plus')
                .addClass("ui-icon-minus");
            $(".tree-plus").trigger("click");
        }
    }
});

更新:我发现您上次发布的onClickGrouploadComplete 的代码是您的性能问题的根源。试试the demo,在您的情况下它有 1000 行而不是 120 行。 1000 行的扩展速度很慢,但在我的计算机上 IE11 大约需要 0.5 秒,而在 Chrome 中大约需要 190 毫秒。这与您报告的 5-6 秒相差甚远。

The next demo 在展开/折叠所有行之前暂时隐藏网格,然后将其显示回来。它额外提高了性能。最后一个演示在我的计算机上展开所有内容在 Chrome 中为 160 毫秒,在 IE11 中为 350 毫秒。

【讨论】:

  • 好的,我已经添加了整个网格。我正在对 groups 数组做很多工作。我所做的是尝试保存每个组的折叠/展开状态,以便当我排序和重新加载网格时,默认情况下它不会折叠所有组。也许您也知道这样做的更好方法。谢谢!
  • @MarkHighfield:在我看来,您上一条评论中的问题与您原来的问题完全无关——在免费 jqGrid 中展开/折叠 TreeGrid 的性能。看the old answer,我在这里描述了保存TreeGrid 状态的方法。我看到您再次修改了问题的代码。现在可以看到您使用数据分组而不是 TreeGrid。抱歉,但您应该在一开始就将问题表述得足够清楚。我无法为您创建很多不同的演示来猜测您真正需要什么。
  • 如果我的措辞不明确,我深表歉意。我只是想提高分组的展开/折叠所有按钮的性能
  • 根据发布的代码知道为什么展开/折叠所有性能如此缓慢?
  • @MarkHighfield:我发现你使用的onClickGrouploadComplete的代码是你性能问题的根源。试试the demo,在您的情况下它有 1000 行而不是 120 行。 1000 行的扩展速度很慢,但在我的计算机上 IE11 大约需要 0.5 秒,而在 Chrome 中大约需要 190 毫秒。这与您报告的 5-6 秒相差甚远。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 2013-06-06
相关资源
最近更新 更多