Reputation: 71
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.
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,
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.
Upvotes: 5
Views: 1599
Reputation: 4074
jQuery datatables is coded to use the success callback in ajax and it breaks if you intercept it. Source
You can also make use of 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);
}
}
});
Note: return string, not json in dataFilter callback.
Upvotes: 1
Reputation: 71
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.
Upvotes: 2