Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correctly Suppressing Warnings in DataTables?

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.

like image 571
jab Avatar asked Aug 13 '12 20:08

jab


People also ask

How to disable warning message in Datatable?

Just add $. fn. dataTableExt. sErrMode = 'throw' to the page where DataTables will be placed.

How do I remove table data warning?

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.

What is a DataTables warning?

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.


2 Answers

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.

like image 127
orad Avatar answered Sep 23 '22 19:09

orad


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);   } } 
like image 33
Taylor Avatar answered Sep 22 '22 19:09

Taylor