网格中的DWR数据加载需要时间

时间:2014-03-20 07:54:46

标签: jquery performance jqgrid dwr

我有一个DWR调用,它在网格中加载5000条记录。加载需要10秒。我想尽量减少它。我在标题中还有另一个dwr调用,它会加载两个下拉列表。我删除了下拉列表并检查了性能。即使这样,加载也需要很长时间。有人可以帮助我减少加载网格所需的时间吗?

以下是我的代码。

DataHandler.getAssignedTasks(Id, Role, Type, city, Flag, Loc, Month, cat, Act, 
        { callback : function(data){    
    /**
     * Setting result values into local variable
     */
    resultSet = data;
    var Grid = $("#Grid");

    /**
     * Deciding the sorting format according to the Date format.
     */ 
    var sortingType = "";
    if("yy/mm/dd" == gUserDateFormat)
    {
        sortingType = 'text';            
    }
    else if("dd/mm/yy" == gUserDateFormat)
    {
        sortingType = 'date';
    }

    /**
     * Configure the grid for the required fields with values and load the data in
     * the DIV 'taskGrid'
     */

    Grid.jqGrid({
        datatype : "local",
        data : resultSet,
        height : (gridRows * 7) + 5,
        width : pageInnerWidth - (pageInnerWidth * 0.1225),
        sortable : false,
        ignoreCase : true,
        sortorder : "desc",
        colNames : ['Title', 'Location','Act', 'mat', 'Date', 'Status', 'Action', 'Aging', 'Imp', 'Mon', 'Rev', 'Type', 'city'],
        colModel : [{
            name : 'title',
            index : 'title',
            formatter : function(cellValue, rowObject) {
                return '<a href="taskDetails?taskId=' + rowObject.rowId + '" target="_blank">' + cellValue + '</a>';
            },
            width : '160',
            align : 'left',
            sorttype : 'text'
        },{
            name : 'cLocation',
            index : 'cLocation',
            width : '275',
            align : 'left',
            sorttype : 'text',
            hidden : hideLocationData
        },{
            name : 'act',
            index : 'act',
            width : '160',
            align : 'left',
            sorttype : 'text'
        }, {
            name : 'mat',
            index : 'mat',
            width : '160',
            align : 'left',
            sorttype : 'text'
        },{
            name : 'date',
            index : 'date',
            width : '120',
            align : 'left',
            sorttype : sortingType, 
            datefmt: 'd/m/Y',
            sortable: true,
            formatoptions: {srcformat: 'd/m/Y', newformat:'d/m/Y'}
        }, {
            name : 'status',
            index : 'status',
            width : '110',
            align : 'left',
            sorttype : 'text'
        }, {
            name : 'actionLink',
            index : 'actionLink',
            formatter : function(cellValue, rowObject) {
                return '<a href="companyDetails?taskId=' + rowObject.rowId + '" target="_blank">' + cellValue + '</a>';
            },
            width : '80',
            align : 'left',
            sortable : false,
            searchable : false,
            hidden : hideActionLink
        }, {
            name : 'aging',
            index : 'aging',
            width : '70',
            align : 'left',
            sorttype : 'numeric'
        }, {
            name : 'imp',
            index : 'imp',
            width : '120',
            align : 'left',
            sorttype : 'text',
            hidden : hideimpData
        }, {
            name : 'mon',
            index : 'mon',
            width : '120',
            align : 'left',
            sorttype : 'text',
            hidden : hideMonData
        }, {
            name : 'revName',
            index : 'revName',
            width : '120',
            align : 'center',
            sorttype : 'text',
            hidden : hideRevData
        }, {
            name : 'typeName',
            index : 'typeName',
            width : '5',
            align : 'center',
            sorttype : 'text',
            hidden : true,
            searchoptions : { searchhidden : true }
        }, {
            name : 'cityName',
            index : 'cityName',
            width : '5',
            align : 'center',
            sorttype : 'text',
            hidden : true,
            searchoptions : { searchhidden : true }
        }
        ],
        rowList : [ 15, 30, 50, 75, 100 ],
        pager : '#taskPager',
        rowNum : gridRows,
        altRows : true,
        altclass : "myclass",
        viewrecords : true
    });

    if (!searchEnabledOnTaskGrid)
    {
        /**
         * Configure the grid 'taskGrid' to enable search on fields
         */
        taskGrid.jqGrid('filterToolbar',{
            searchOnEnter : false,
            defaultSearch : "cn",
            beforeSearch : function(){
                if (specialCharactersCheck($("#gs_title").val())
                    || specialCharactersCheck($("#gs_cLocation").val())
                    || specialCharactersCheck($("#gs_act").val())
                    || specialCharactersCheck($("#gs_mat").val())
                    || specialCharactersCheck($("#gs_date").val())
                    || specialCharactersCheck($("#gs_statusText").val())
                    || specialCharactersCheck($("#gs_actionLink").val())
                    || specialCharactersCheck($("#gs_aging").val())
                    || specialCharactersCheck($("#gs_impName").val())
                    || specialCharactersCheck($("#gs_monName").val())
                    || specialCharactersCheck($("#gs_revName").val())
                    || specialCharactersCheck($("#gs_typeName").val())
                    || specialCharactersCheck($("#gs_cityName").val()))
                {
                    return true;
                }
                else
                {
                    valtypeName = $("#typeName option:selected").text();
                    if (valTaskType == "Show All")
                    {
                        valtypeName = "";
                    }

                    valcity = $("#city option:selected").text();
                    if (valcity == "Show All")
                    {
                        valcity = "";
                    }

                    var myGrid = $("#taskGrid");
                    var myFilter = {groupOp: "AND", mats: []};
                    var postData = myGrid.jqGrid('getGridParam','postData');
                    var searchData = jQuery.parseJSON(postData.filters);
                    for (var iMat = 0; iMat < searchData.rules.length; iMat ++)
                    {
                        myFilter.mats.push({ field: searchData.mats[iMat ].field, op: "cn", data: searchData.mats[iMat ].data });
                    }
                    if (valtypeName .length !== 0)
                    {
                        myFilter.mats.push({ field: "typeName", op: "cn", data: valtypeName  });
                    }
                    if (valcity.length !== 0)
                    {
                        myFilter.mats.push({ field: "cityName", op: "cn", data: valcity });
                    }
                    myGrid[0].p.search = true;
                    $.extend(myGrid[0].p.postData, { filters: JSON.stringify(myFilter) });
                    myGrid.trigger("reloadGrid", [{ page: 1, current: true}]);
                    return true;
                }
            }
        });

        searchEnabledOnTaskGrid = true;
    }
        },
    errorHandler : function(){
    },
    async : false
});

无论如何要减少加载所需的时间?

1 个答案:

答案 0 :(得分:1)

我认为加载10秒的问题不在加载 5000行中。唯一的问题是您使用rowNum : gridRows并尝试一次显示所有行。显示器无法显示所有5000行,您将花费99%的时间填充页面中不可见的部分。我建议您为rowNum设置一些更合理的值,例如rowNum: 50。我建议您另外使用height: "auto"而不是height : (gridRows * 7) + 5。用户可以使用本地分页,排序和过滤。在这种情况下,所有(分页,排序和过滤)都可以很快地运行。

我无论如何我都会严格建议添加gridview: true作为网格选项,以提高网格性能,而不会有任何其他缺点。有关其他信息,请参阅the answer