Suppress warning in case of custom handling of ajax.dataSrc
Suppress warning in case of custom handling of ajax.dataSrc
I am loading data into tables from the server via ajax.
I do so with a call to special functions that return the data I need.. OR a detailed error if there is some problem.
I then want to show the error to the user, in a nice format.
All of the above was easily achieved with the code at the bottom..
However, I still get a warning displayed by dataTables itself, saying: "DataTables warning: table id=MFF_YouDontFollow - [Object Object]" (in the scenario that the server response is of an "error" json structure).
Now, this is despite the fact that the actual data returned by the handling function is actually valid.
The warning from dataTables seems be unrelated to the actual data returned by the dataSrc function. Even when I return actual real "fake" data, which DOES get shown properly in the table, the warning is displayed. Seems like the warning with regards to the fact that the ORIGINAL data is not valid, rather than the data returned by dataSrc.
Am I doing it wrong? Is there a better way to handle such a scenario when the response is either "data" or "warning" structure?
Here's the code I use:
dataSrc : function ( json ) {
if (json.error) //Indicates that the returned result is an error
{
//My nice error notification
$.smallBoxCountdown({
title : "Attention!",
content : json.error.message,
color : "#C46A69",
iconSmall : "fa fa-2x fa-warning bounce animated",
timeout : 15*1000,
});
//Returning empty
return [];
}
else
{return json.data;}
Answers
Try returning just
[]
.ajax.dataSrc
doesn't handle the server-side processing parameters at the moment, just the data.Allan
returning [] from where? The server? I can't. The returning function is an API function that returns an error structure, so that I can know why no data is returned.
Is there nothing I can do to "intercept" responses from the server, analyze them and react accordingly?
Before I'm fairly sure you have it returning an object there, or am I incorrect?
Doing a simply
return []
; is what I was trying to suggest certainly. Does that not work? Can you link tot he page showing the issue please, that should work.Allan
as I said, it "works" in the sense that the table shows up as empty.
BUT STILL, dataTables shows the warning. This is because it processes the response (including perhaps looking for the filtered count in recordsTotal and recordsFiltered)..
So while I can "manipulate" the data in the dataSrc function, I can't intercept the handling of the original response from the server.
Anyway to do that?
Sorry for the misunderstanding, I think I've got it now :-)
Just this morning I've committed a change to DataTables that I think will fit your needs - in 1.10.7 (which I'm going to package up shortly) the
xhr
event will trigger for both success and error code branches. Furthermore, you will be able to returntrue
to have it not trigger DataTables own error handling (i.e. tell it that you've done it yourself).So you might do something like:
This is currently in the nightly version on the download page if you want to try it immediately.
Allan