I'm trying to correctly suppress warnings (alerts) in DataTables. The standard behavior of DataTables is to throw a javascript alert when an error occurs; however, this is currently inconvenient for me. I have been trying to convert the warning to a javascript error by
$.fn.dataTableExt.sErrMode = 'throw';
Which works correctly, but this stops the current javascript execution, which is not what I want. So, I wrapped the DataTables operations (init and changes) in a try-catch with no error handling; however, this also halts the javascript execution. (Tested on Chrome and Firefox)
My question is how do I go about getting rid of these errors/alerts for the purposes of debugging? I'm trying to debug other parts of my script, but these alerts keep on getting in the way.
Just add $. fn. dataTableExt. sErrMode = 'throw' to the page where DataTables will be placed.
If you just want to get rid of the alert box (eg "stop the warning") add this as the first line of your $(document). ready : $. fn.
DataTables warns about the use of them by default, as the majority of the time they are not intended for display - for example, rather than showing null in the table, you might want to show Not yet set, or simply an empty string (empty cell). For this, DataTables has a columns. defaultContent option.
I modified the native alert using this closure function to redirect DataTables warnings to the console.
window.alert = (function() { var nativeAlert = window.alert; return function(message) { window.alert = nativeAlert; message.indexOf("DataTables warning") === 0 ? console.warn(message) : nativeAlert(message); } })();
It restores the window.alert
to its native function on first trigger. If you don't want it to restore to the original alert, just comment out the window.alert = nativeAlert;
line.
Here's a solution proposed here that's slightly modified and works in v1.10.2 without having to change any vendor files:
$.fn.dataTableExt.sErrMode = "console"; $.fn.dataTableExt.oApi._fnLog = function (oSettings, iLevel, sMesg, tn) { var sAlert = (oSettings === null) ? "DataTables warning: "+sMesg : "DataTables warning (table id = '"+oSettings.sTableId+"'): "+sMesg ; if (tn) { sAlert += ". For more information about this error, please see "+ "http://datatables.net/tn/"+tn ; } if (iLevel === 0) { if ($.fn.dataTableExt.sErrMode == "alert") { alert(sAlert); } else if ($.fn.dataTableExt.sErrMode == "thow") { throw sAlert; } else if ($.fn.dataTableExt.sErrMode == "console") { console.log(sAlert); } else if ($.fn.dataTableExt.sErrMode == "mute") {} return; } else if (console !== undefined && console.log) { console.log(sAlert); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With