为什么不允许在jquery datatable服务器端处理ajax成功使用?

时间:2021-07-06 14:22:29

I am using asp.net mvc5 and trying to use jquery datatable plugin server-side processing. The tutorials of server side processing show a format for returning result from server.But the difference in my project is that i cannot send a typed array for 'data' from server. I am sending whole tbody as string with all html tags. My datatable code as below.

我正在使用asp.net mvc5并尝试使用jquery datatable插件服务器端处理。服务器端处理的教程显示了从服务器返回结果的格式。但是我的项目的不同之处在于我无法从服务器发送“数据”的类型化数组。我发送整个tbody作为所有html标签的字符串。我的数据表代码如下。

  var e = t.DataTable({processing: true, 
        serverSide: true,
        info: true,   
        stateSave: true, 
        PaginationType: "full_numbers",
        ajax:{
            "url": '@Url.Action("AjaxGetJsonData","Define",new { tablename= @HttpContext.Current.Request.QueryString["tablename"] })',
            "type": "GET",
            "success": function (result) {
                console.log(result);

                $("#sample_1").find("tbody").html(result.data);
                $("#sample_1_processing").hide();
                Init();
                //var oSettings = $("#sample_1").dataTable().fnSettings();
                //oSettings.iTotalRecords = result.recordsTotal;

            }

        }

The Result of ajax is something like as below,

ajax的结果如下所示,

Object {draw: 1, iTotalRecords: 25000, iTotalDisplayRecords: 0, data: Array[1]}

the data is like

数据就像

<tr><td></td><td></td><td></td><td></td></tr>

because the view is generic for many tables and there are many situation that i should control.Thus, i am using StringBuilder in server side. If i put success to ajax the pagination elements disappear at the bottom of datatable. Why it is not allowed to use success in ajax? i have all total features of datatable and is there any way to set features like iTotalRecords manually ? I know here is not datatable forum. I am sorry about this but i spent time a lot and cannot find solution. I want to handle all features of datatable in ajax success manually.I am using last version of datatable.

因为视图对于许多表是通用的,并且我应该控制许多情况。因此,我在服务器端使用StringBuilder。如果我把成功放到ajax上,那么分页元素就会消失在数据表的底部。为什么不允许在ajax中使用成功?我有数据表的所有功能,有没有办法手动设置iTotalRecords等功能?我知道这里不是数据表论坛。我很抱歉这个,但我花了很多时间,找不到解决方案。我想手动处理ajax成功中数据表的所有功能。我正在使用最新版本的数据表。

2 个解决方案

#1


2  

I have solved my problem at the end.There is something interesting but i can use success now.

我在最后解决了我的问题。有一些有趣但我现在可以成功使用。

var e = t.DataTable({
        processing: true,
        serverSide: true,
        info: true,
        stateSave: true,
        PaginationType: "full_numbers",
        "sAjaxSource": '@Url.Action("AjaxGetJsonData","Define")',
        "fnServerData": function (sSource, aoData, fnCallback) {
            aoData.push({ "name": "tablename", "value": $("#temprory").val() });
            console.log(aoData);
            $.ajax({
                url: sSource,
                type: "POST",
                data: aoData,
                success: function (msg) {
                    fnCallback(msg);
                    console.log(msg);
                    $("#sample_1").find("tbody").html(msg.data);
                    $("#sample_1_processing").hide();
                    Init();
                }
            });
        }

The interesting point is that if you remove fnCallback(msg), the below part of datatable that includes pagination disappears. I dont know exactly what it does but this solved my problem.

有趣的是,如果你删除fnCallback(msg),包含分页的数据表的下面部分就会消失。我不确切知道它做了什么,但这解决了我的问题。

#2


0  

jQuery datatables is coded to use the success callback in ajax and it breaks if you intercept it. Source

jQuery数据表被编码为使用ajax中的成功回调,如果你拦截它就会中断。资源

You can also make use of jQuery ajax dataFilter callback.

您还可以使用jQuery ajax dataFilter回调。

$('table[id=entries]').DataTable({
    processing: true,
    serverSide: true,
    ajax: {
        type: 'POST',
        url: 'http://example.com/entries',
        dataFilter: function(response) {
            var json_response = JSON.parse(response);
            if (json_response.recordsTotal) {
                // There're entries;
            }else{
                // There're no entries;
            }
            return response;
        },
        error: function (xhr) {
            console.error(xhr.responseJSON);
        }
    }
});

Note: return string, not json in dataFilter callback.

注意:返回字符串,而不是dataFilter回调中的json。

#1


2  

I have solved my problem at the end.There is something interesting but i can use success now.

我在最后解决了我的问题。有一些有趣但我现在可以成功使用。

var e = t.DataTable({
        processing: true,
        serverSide: true,
        info: true,
        stateSave: true,
        PaginationType: "full_numbers",
        "sAjaxSource": '@Url.Action("AjaxGetJsonData","Define")',
        "fnServerData": function (sSource, aoData, fnCallback) {
            aoData.push({ "name": "tablename", "value": $("#temprory").val() });
            console.log(aoData);
            $.ajax({
                url: sSource,
                type: "POST",
                data: aoData,
                success: function (msg) {
                    fnCallback(msg);
                    console.log(msg);
                    $("#sample_1").find("tbody").html(msg.data);
                    $("#sample_1_processing").hide();
                    Init();
                }
            });
        }

The interesting point is that if you remove fnCallback(msg), the below part of datatable that includes pagination disappears. I dont know exactly what it does but this solved my problem.

有趣的是,如果你删除fnCallback(msg),包含分页的数据表的下面部分就会消失。我不确切知道它做了什么,但这解决了我的问题。

#2


0  

jQuery datatables is coded to use the success callback in ajax and it breaks if you intercept it. Source

jQuery数据表被编码为使用ajax中的成功回调,如果你拦截它就会中断。资源

You can also make use of jQuery ajax dataFilter callback.

您还可以使用jQuery ajax dataFilter回调。

$('table[id=entries]').DataTable({
    processing: true,
    serverSide: true,
    ajax: {
        type: 'POST',
        url: 'http://example.com/entries',
        dataFilter: function(response) {
            var json_response = JSON.parse(response);
            if (json_response.recordsTotal) {
                // There're entries;
            }else{
                // There're no entries;
            }
            return response;
        },
        error: function (xhr) {
            console.error(xhr.responseJSON);
        }
    }
});

Note: return string, not json in dataFilter callback.

注意:返回字符串,而不是dataFilter回调中的json。