jqGrid扩展 重新加载表格数据,数据源从C#的webservice

先上代码:

$.jgrid.extend({
    refresh: function (param) {
        var $_t = $(this);
        var defaults = { sortname: $_t.jqGrid("getGridParam", "sortname"), callback: null, url: '', postData: {}, isXls: false };
        var opts = $.extend(defaults, param);
        //param= $.extend( { sortname: '', callback: null, url: '' },param);

        $("input[type='button']").prop("disabled", true);
        var jsonReader = {};
        if (opts.url.indexOf(".asmx") > -1) {
            jsonReader = { root: "d.Table.rows", page: "d.Page", total: "d.Total", records: "d.Records", userdata: "d.Userdata" };
        }
        else {
            jsonReader = { root: "rows", page: "page", total: "total", records: "records" };
        }
        var _loadPage = $_t.jqGrid("getGridParam", "page");
        if (!_loadPage) _loadPage = 1;
        $_t.jqGrid("setGridParam", {
            url: opts.url, jsonReader: jsonReader,
            datatype: "json", page: _loadPage, sortname: opts.sortname,
            ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
            postData: opts.postData,
            gridComplete: function () {
                if (param.callback) param.callback($_t);
            },
            loadComplete: function (data) {
                if (param.loadComplete) param.loadComplete(data);
            }
        }).trigger("reloadGrid");

        if (opts.isXls) {
            $(".ui-pg-div:contains(导出)", $_t.jqGrid("getGridParam", "pager")).parent().remove();
            $_t.jqGrid('navButtonAdd', $_t.jqGrid("getGridParam", "pager"), {
                caption: "导出", onClickButton: function () {
                    var _url = opts.url;
                    if (opts.url.indexOf('?') > -1) _url += '&'; else _url += '?';
                    if (_url.indexOf('isXls') > -1) { } else {
                        _url += 'isXls=1&';
                    }
                    //colNames colModel 进行处理 传到服务端
                    var _colNames = $_t.jqGrid("getGridParam", "colNames");
                    var _colModel = $_t.jqGrid("getGridParam", "colModel");
                    var _result = {};
                    $.map(_colModel, function (item, k) {
                        _result['xls_' + item.name] = _colNames[k];
                    });
                    _url = _url + $.param(_result);
                    _url = _url + "&page=" + $_t.jqGrid("getGridParam", "page");
                    _url = _url + "&rows=" + $_t.jqGrid("getGridParam", "rowNum");
                    _url = _url + "&" + $.param(opts.postData);
                    window.open(_url);
                }
            });
        }

        $("input[type='button']").prop("disabled", false);
        $(".ui-jqgrid-bdiv div").css("position", "");
    }
});

function loadFilter(data) {
    /// <summary>jqGrid加载JSON数据时,对返回的data统一处理方式函数</summary>
    /// <param name="data">json返回的JsonData对象</param>
    /// <returns>无返回</returns>
    if (!data.d) {
        data.d = { Table: {} };
    }
    if (!data.d.Table) data.d.Table = {};
    if (data.d.Total)
        data.d.Table.total = data.d.Total;
    else
        data.d.Table.total = 0;
    if (!data.d.Table.rows) data.d.Table.rows = [];
    return data.d.Table;
}

使用方法:

$("#tblList").jqGrid("refresh", { url: "/Task/Services/S_Task.asmx/GetLists?action='MyJoin'" + param ,isXls:false},callback:function(){


});


在使用jqGrid的时候,刷新加载服务端json数据,需要写很多代码,所以封装了这个方法,可以方便调用。


需要注意Framework要3.5以上。

你可能感兴趣的:(js,jquery,webservice,C#,jqGrid)