代码之家  ›  专栏  ›  技术社区  ›  Bob

JQGrid重绘很慢

  •  3
  • Bob  · 技术社区  · 15 年前

    电网速度 直到我开始优化,才真正困扰我。

    如果发现即使我有一个小网格(每页20个项目),悬停高亮显示是缓慢的,如果网格恰好需要一个滚动条在页面上,滚动页面是非常缓慢的。

    我用的是jquery.ui.themecss。

    为了确保页面上没有其他内容,我将网格的显示设置为“无”,页面像往常一样快速运行——正如我所怀疑的那样。

    使用IE8调试器,我使用profiler来测量我的javascript和html的速度,这些速度非常快——所以它肯定是屏幕上的网格图。

    我很感激任何和所有的帮助或建议,以提高性能。

    我会附上我的网格定义(它相当大),以防你可能认为它可能存在:

    $("#tblVariables").jqGrid({
            url: '/StudyAdmin/GetVariable.aspx?FilterType=' + Filter + '&SelectedFolder=' + SelectedFolder + '&SelectedGroup=' + SelectedGroup,
            datatype: "json",
            mtype: "POST",
            colNames: ['Variable Name', 'Hidden Original Text', 'Original Text', 'Label', 'Hidden', 'Groups', 'HiddenGroups'],
            colModel: [
                { name: 'VarName', index: 'VarName', editable: false },
                { name: 'VarOriginalText', index: 'VarOriginalText', hidden: true, editable: true, editrules: { edithidden: true} },
                { name: 'VarOriginalTextToShow', index: 'VarOriginalTextToShow', editable: false, sortable: false },
                { name: 'VarReportingText', index: 'VarReportingText', editable: true },
                { name: 'HiddenVar', index: 'HiddenVar', editable: true, edittype: "select", editoptions: { value: { "true": "True", "false": "False"}} },
                { name: 'Groups', index: 'Groups', editable: false },
                { name: 'HiddenGroups', index: 'HiddenGroups', hidden: true, editable: true, editrules: { edithidden: true} }
            ],
            editurl: "/StudyAdmin/Editvariable.aspx",
            height: "100%",
            gridComplete: function () {
                var grid = jQuery("#tblVariables");
                var ids = grid.jqGrid('getDataIDs');
                for (var i = 0; i < ids.length; i++) {
                    var rowId = ids[i];
                    var splitGroups = $('#tblVariables').getCell(rowId, 'HiddenGroups').split(",");
                    if (splitGroups.length == 1 && splitGroups[0] == "") splitGroups = "";
                    var GroupSelect = "<select id='Group_" + rowId + "' onchange='filterGroup(this)'>";
                    if (splitGroups.length > 0) GroupSelect += "<option value='HasSelectGroup'>Select Group</option>";
                    else GroupSelect += "<option value='NotHasSelectGroup'>No Groups</option>";
                    for (var k = 0; k < splitGroups.length; k++) {
                        GroupSelect += "<option value='" + splitGroups[k] + "'>" + splitGroups[k] + "</option>";
                    }
                    //add all groups in here
                    GroupSelect += "</select>";
                    grid.jqGrid('setRowData', rowId, { Groups: GroupSelect });
                }
                setGridWidth($("#VariableGridContentConainer").width() - 19);
            },
            afterInsertRow: function (rowid, rowdata, rowelem) {
                $("#" + rowid).addClass('jstree-draggable');
            },
            ondblClickRow: function (id, ri, ci) {
                lastSelRow = id;
                $("#tblVariables").jqGrid('editRow', id, true);
            },
            onSelectRow: function (id) {
                if ($('#QuestionTree').find("#FQ" + id).attr("id") !== undefined) {
                    $.jstree._focused().select_node("#FQ" + id);
                }
                if (id == null) {
                    //$("#tblAnswers").setGridParam({ url: "/StudyAdmin/GetAnswers.aspx", page: 1 }).trigger('reloadGrid');
                    $('#tblAnswers').GridUnload();
                    loadAnswersGrid("-1");
                } else {
                    //$("#tblAnswers").setGridParam({ url: "/StudyAdmin/GetAnswers.aspx?id=" + id, page: 1 }).trigger('reloadGrid');
                    $('#tblAnswers').GridUnload();
                    loadAnswersGrid(id);
                    if (id && id !== lastSelRow) $("#tblVariables").jqGrid('saveRow', lastSelRow);
                }
            },
            viewrecords: true,
            rowNum: 20,
            loadonce: true,
            pager: jQuery('#pagernav'),
            sortname: 'VarName',
            sortorder: "asc",
            imgpath: '/Content/Images/',
            jsonReader: {
                root: "rows",
                page: "page",
                total: "total",
                records: "records",
                repeatitems: true,
                cell: "cell",
                id: "id"
            }
        });
    
    1 回复  |  直到 15 年前
        1
  •  9
  •   Community Mohan Dere    9 年前

    您没有发布完整的JavaScript代码,所以我写了一些注释,说明如何基于现有信息优化jqGrid。

    1. afterInsertRow 做同样的工作 gridComplete loadComplete . 然后你可以加上 gridview: true jqgrid, firefox and css- text-decoration problem 更多细节)。你现在的行动 后插入行 jQuery('.jqgrow',jQuery('#tblVariables')).addClass('jstree-draggable'); 加载完成 网格完成 .
    2. 你构造了 Groups HiddenGroups 列。从服务器发送的数据,作为 列似乎被覆盖。可以构造相同的包含 custom formatter . 也许你可以去掉不需要的 一点也不。看看 jqGrid: Editable column that always shows a select 例如自定义格式化程序和 Add multiple input elements in a custom edit type field 例如 custom editing custom unformater 你也会感兴趣的。
    3. formatter:'checkbox' HiddenVar 发送的数据。
    4. 如果删除一些默认值(如 jsonReader )或不推荐的值,如 imgpath http://www.trirand.com/jqgridwiki/doku.php?id=wiki:upgrade_from_3.4.x_to_3.5 )并从中删除默认值 colModel (就像 editable: false ).

    what is the best way to optimize my json on an asp.net-mvc site .

    :可能您不需要在 . 相反,只有选中行或处于编辑模式时,才能执行此操作。从整体中删除选定元素的构造 可以提高网格的性能并简化代码。你可以用 editoptions 具有 value 作为功能或使用其他可能性 editoptions onchange dataEvents 具有 type: 'change' .