为什么不允许在 jquery 数据表服务器端处理 ajax 中使用成功?
Why not allowed using success in jquery datatable server side processing ajax?
我正在使用 asp.net mvc5 并尝试使用 jquery 数据表插件服务器端处理。服务器端处理教程显示了从 server.But 返回结果的格式,我的项目的不同之处在于我无法从服务器发送 'data' 的类型化数组。我将整个 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;
}
}
ajax 的结果如下所示,
Object {draw: 1, iTotalRecords: 25000, iTotalDisplayRecords: 0, data: Array[1]}
数据就像
<tr><td></td><td></td><td></td><td></td></tr>
因为该视图对于许多表都是通用的,而且在很多情况下我应该 control.Thus,所以我在服务器端使用 StringBuilder。如果我将成功设置为 ajax,则分页元素会消失在数据表的底部。为什么在ajax中不允许使用success?我拥有数据表的所有全部功能,有没有办法手动设置 iTotalRecords 等功能?我知道这里不是数据表论坛。我对此感到抱歉,但我花了很多时间却找不到解决方案。我想在 ajax 成功中处理数据表的所有功能 manually.I 我正在使用最新版本的数据表。
我已经解决了我的问题 end.There 很有趣,但我现在可以使用成功了。
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),下面包含分页的数据表部分就会消失。我不知道它到底做了什么,但这解决了我的问题。
jQuery 数据表被编码为使用 ajax 中的成功回调,如果您拦截它,它就会中断。 Source
您也可以使用 jQuery ajax dataFilter callback。
$('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);
}
}
});
注意:return 字符串,而不是 dataFilter 回调中的 json。
我正在使用 asp.net mvc5 并尝试使用 jquery 数据表插件服务器端处理。服务器端处理教程显示了从 server.But 返回结果的格式,我的项目的不同之处在于我无法从服务器发送 'data' 的类型化数组。我将整个 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;
}
}
ajax 的结果如下所示,
Object {draw: 1, iTotalRecords: 25000, iTotalDisplayRecords: 0, data: Array[1]}
数据就像
<tr><td></td><td></td><td></td><td></td></tr>
因为该视图对于许多表都是通用的,而且在很多情况下我应该 control.Thus,所以我在服务器端使用 StringBuilder。如果我将成功设置为 ajax,则分页元素会消失在数据表的底部。为什么在ajax中不允许使用success?我拥有数据表的所有全部功能,有没有办法手动设置 iTotalRecords 等功能?我知道这里不是数据表论坛。我对此感到抱歉,但我花了很多时间却找不到解决方案。我想在 ajax 成功中处理数据表的所有功能 manually.I 我正在使用最新版本的数据表。
我已经解决了我的问题 end.There 很有趣,但我现在可以使用成功了。
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),下面包含分页的数据表部分就会消失。我不知道它到底做了什么,但这解决了我的问题。
jQuery 数据表被编码为使用 ajax 中的成功回调,如果您拦截它,它就会中断。 Source
您也可以使用 jQuery ajax dataFilter callback。
$('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);
}
}
});
注意:return 字符串,而不是 dataFilter 回调中的 json。