Javascript 为什么不允许在jquerydatatable服务器端处理ajax中使用success?
我正在使用asp.NETMVC5并尝试使用jquery数据表插件服务器端处理。服务器端处理教程显示了从服务器返回结果的格式。但我的项目中的不同之处在于,我无法从服务器发送“数据”的键入数组。我发送整个tbody作为字符串,带有所有html标记。我的数据表代码如下Javascript 为什么不允许在jquerydatatable服务器端处理ajax中使用success?,javascript,jquery,asp.net-mvc,datatables,server-side-rendering,Javascript,Jquery,Asp.net Mvc,Datatables,Server Side Rendering,我正在使用asp.NETMVC5并尝试使用jquery数据表插件服务器端处理。服务器端处理教程显示了从服务器返回结果的格式。但我的项目中的不同之处在于,我无法从服务器发送“数据”的键入数组。我发送整个tbody作为字符串,带有所有html标记。我的数据表代码如下 var e = t.DataTable({processing: true, serverSide: true, info: true, stateSave: true,
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;
}
}
ajax的结果如下所示:
Object {draw: 1, iTotalRecords: 25000, iTotalDisplayRecords: 0, data: Array[1]}
数据如下
<tr><td></td><td></td><td></td><td></td></tr>
因为视图对于许多表都是通用的,并且有许多情况我应该控制。因此,我在服务器端使用StringBuilder。如果我将成功放在ajax上,分页元素将消失在datatable的底部。为什么不允许在ajax中使用success?我有datatable的全部功能,有没有办法手动设置像iTotalRecords这样的功能?我知道这里不是datatable论坛。对此我很抱歉,但我花了很多时间,无法找到解决方案。我想在ajax success中手动处理datatable的所有功能。我使用的是datatable的最新版本。我在最后解决了我的问题。有一些有趣的东西,但我现在可以使用success
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();
}
});
}
有趣的是,如果删除fnCallback(msg),datatable下面包含分页的部分将消失。我不知道它到底做了什么,但这解决了我的问题。jQuery datatables被编码为使用ajax中的成功回调,如果您拦截它,它就会中断 你也可以利用
注意:在dataFilter回调中返回字符串,而不是json。为什么不在Datatable中使用PartialView?@Alorika感谢您的建议。我已经在使用表的部分视图。顺便说一下,Init()与问题无关。这与数据表无关。
$('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);
}
}
});