0

我正在使用 JQuery DataTables插件来处理我的表,最近我切换到服务器端分页和过滤。特别是,我有一个返回数据以填充客户表的 Web 方法:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCustomers(string jsonAOData, int mode)
    {
        // ...
    }

在我的页面中,我使用此代码通过 AJAX 调用检索数据。

var grid = $('#grid').dataTable({
        bJQueryUI: true,
        bLengthChange: false,
        iDisplayLength: listItemsPerPage,
        bDestroy: true,
        "bProcessing": true,
        "bSort": true,
        "sPaginationType": "full_numbers",
        "bServerSide": true,
        "sAjaxSource": "/wsData/GetData.asmx/GetCustomers",
        "fnServerData": function (sSource, aoData, fnCallback) {

            var jsonAOData = JSON.stringify(aoData);

            $.ajax({
                //dataType: 'json', 
                contentType: "application/json; charset=utf-8",
                type: "POST",
                url: sSource,
                data: "{jsonAOData : '" + jsonAOData + "', mode:'0'}",
                success: function (msg) {
                    fnCallback(JSON.parse(msg.d));
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.responseText);
                }
            });
        },
        "aoColumnDefs": [
            // my columns structure
        ]
    });

如您所见,我向 web 方法传递了两个参数:jsonAOData,包含用于分页和过滤的所有信息,以及mode,它定义了如何从 DB 中获取数据。它工作得很好,但现在我需要重新加载表中的数据,将不同的值传递给我的 web 方法mode

阅读我发现函数的文档fnReloadAjax()可以帮助我,但我找不到将它应用于我的问题的正确方法。

我试过这样:

grid.fnReloadAjax("/wsData/GetData.asmx/GetCustomers?mode=1");

但它不起作用。你能帮助我吗?我在哪里做错了?

如何将新参数传递给我的网络方法?

4

2 回答 2

0

无法立即发现丢失/错误的内容 - 但这是我的有效版本。

$(document).ready(function () {
        jQuery.support.cors = true;

        var sAjaxSourceUrl = '@ViewBag.sAjaxSourceUrl' //passing mine from MVC3 viewbag, but you can hard-code it
        var dt = $('#dataTable').dataTable({
            "bProcessing": true,
            "bSort": true,
            "bServerSide": true,
            "sServerMethod": "POST",
            "sAjaxSource": sAjaxSourceUrl,
            "fnServerData": function (sSource, aoData, fnCallback) {
                var jsonAOData = JSON.stringify(aoData);
                $.ajax({
                    crossDomain: true,
                    type: "POST",
                    url: sSource,
                    data: jsonAOData,
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        fnCallback($.parseJSON(data));
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("Status: " + XMLHttpRequest.status + "\r\n" + textStatus + "\r\n" + errorThrown);
                    }
                });
            },
            "aoColumnDefs": [
// my columns structure
],
            "sScrollY": "500",
            "bScrollCollapse": true
        });
于 2013-01-17T10:42:53.530 回答
0

我发现fnReloadAjax()对我来说不是那么好。所以在一些论坛之后我决定使用fnDraw().

mode我根据要检索的数据定义了一个全局变量。然后我调用fnDraw(). 该表是从 web 方法加载数据重新绘制的。

在 AJAX 调用中我设置:

data: "{jsonAOData : '" + jsonAOData + "', mode:'" + mode +"'}",
于 2013-01-17T14:48:16.977 回答