jQgrid的gridComplete事件正在触发但是loadComplete没有触发.....为什么?

时间:2013-08-10 11:00:41

标签: jquery jqgrid jqgrid-asp.net

在我的ASP.NET项目中,我正在集成jQgrid来显示数据。我的问题是loadComplete事件没有触发。我的整个jQgrid代码如下:

function getData(jqGridParams) 
    {
        var sortcookieval = $.cookie("ItemGridSortInfo");
        var sname = "";
        var sorder = "";
        if (sortcookieval != null) 
        {
            var sortInfo = sortInfoFromCookie("ItemGridSortInfo");
            sname = sortInfo.sortname;
            sorder = sortInfo.sortorder;
        }
        else 
        {
            sname = "";
            sorder = "asc";
        }
        var params = new Object();
        params.pageIndex = jqGridParams.page;
        params.pageSize = jqGridParams.rows;
        params.sortIndex = sname; //jqGridParams.sidx;  
        params.sortDirection = sorder;  //jqGridParams.sord;
        params._search = jqGridParams._search;
        if (jqGridParams.filters === undefined)
            params.filters = null;
        else
            params.filters = jqGridParams.filters;

        $.ajax({
            url: 'WSAjax.asmx/GetDataForGrid',
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify(params),
            success: function (data, textStatus) 
            {
                if (textStatus == "success") 
                {
                    var grid = $("#ItemGrid")[0];
                    grid.addJSONData(data.d);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) 
            {
                alert(textStatus, errorThrown);
            }
        });
    }

    function saveSortInfoToCookie(name, grid)
    {
        var sortInfo = new Object();
        sortInfo.sortname = grid.jqGrid('getGridParam', 'sortname');
        sortInfo.sortorder = grid.jqGrid('getGridParam', 'sortorder');
        //$('#Hidden').val(JSON.stringify(gridInfo));
        //alert($('#Hidden').val());
        $.cookie(name, JSON.stringify(sortInfo), {expires: 5});
    }

    function sortInfoFromCookie(name) 
    {
        var c = $.cookie(name);
        if (c === null)
            return;
        return $.parseJSON(c);
    }

    $(document).ready(function () {
        var oItemGrid = $("#ItemGrid");
        oItemGrid.jqGrid({
            datatype:
                function (jqGridParams) {
                    getData(jqGridParams);
                },
            colNames: ['Type', 'Name', 'Desc'],
            colModel: [
                { name: 'Type', index: 'Type', width: 40 },
                { name: 'Name', index: 'Name', width: 40 },
                { name: 'Desc', index: 'Desc', width: 40, sortable: false}],
            autowidth: true,
            height: 'auto',
            rowNum: 10,
            rowList: [10, 20, 30, 40],
            viewrecords: true,
            gridview: true,
            autoencode: true,
            ignoreCase: true,
            caption: 'Remember Sorting and Filtering Functionality',
            pager: '#IGPager',
            onSortCol: function (colModel, colName, sortOrder) {
                saveSortInfoToCookie("ItemGridSortInfo", $("#ItemGrid"));
                var storeval = $.cookie("ItemGridSortInfo");
                alert("Saving sort info in cookie: " + storeval);
            },
            gridComplete: function () {
                var prvData = $.cookie("ItemGridSortInfo");
                alert("Reading saved sort info from cookie : " + prvData);

                var pageno = $.cookie("ItemGridPageInfo");
                alert("Reading saved page no. from cookie:" + pageno);
            },
            loadComplete: function (data) {
                if(data != null)
                {
                    alert("Inside loadComplete..");
                }
            },
            //loadonce: true
        }).jqGrid('navGrid', '#IGPager', { edit: false, add: false, del: false }, {}, {}, {}, {}, {});
    });

如果可能的话,请根据我的情景告诉我gridComplete和loadComplete之间的区别....

1 个答案:

答案 0 :(得分:2)

问题是您将datatype设置为function(用于检索数据的自定义函数)而不是local。如果您使用某个函数,那么您应该在自己的实现中调用loadComplete

因此,如果您将其更改为其他类型(如本地),则loadComplete将由jqGrid自动处理。我为你创建了一个演示。

Demo