Reputation: 1300
I am using datatables version 1.10.0 for search capability.
I want to know for server side processing how do i manipulate the response. For example I have a json response that returns with the records to display and also a status.
My requirement is, if there is validation failure on the server side, I do not want to draw the table and instead, render error messages on screen. Otherwise draw the table.
Simple Example below:
$('#result').dataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "search",
"type": "POST",
//"dataSrc": "resultList",
"dataSrc": function ( json ) {
if (json.responseStatus.value == 'Validation Failure') {
//show error messages on screen
//prevent redraw
} else {
//draw the table with the resultList
}
}
"columns": [
{ "data": "referenceNumber"},
{ "data": "fileName" },
{ "data": "documentType" },
{ "data": "uploadType" },
{ "data": "createdBy" },
{ "data": "memberName" },
{ "data": "dateOfBirthStr"},
{ "data": "createdDateStr" },
{ "data": "comment" },
{ "data": "status" }
]
} );
UPDATE: On the server side I return 403:
return new ResponseEntity(searchUploadResponse, HttpStatus.FORBIDDEN);
On the client side:
$('#result').dataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "search",
"type": "POST",
"dataSrc": "resultList",
error: function (jqXHR, textStatus, errorThrown) {
//handle errorThrown in here
alert("error" + jqXHR + " : " + textStatus + " : " + errorThrown);
}
} ,
The result is i can show my error messages. datatables blocks access to the table with the "processing" div.
Upvotes: 0
Views: 2190
Reputation: 13213
I would return a different status code on the json file instead of a 200
Success
status so the datatable doesn't load any data.
Then, I can catch the error code using .ajaxError()
and do what I'd like to do when that error code appears.
For example, you can return a 503
code status then on the same page as datatabes, you can do something like this:
$("#result").ajaxError(function(event, jqxhr, request, settings){
if(jqxhr.status == 503) alert("Validation Failure");
});
So you can handle the event, since datatables will just blank out if it doesn't get a 200 status code response from the server.
Upvotes: 1