Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  1] [ 2]  / answers: 1 / hits: 21538  / 12 Years ago, mon, august 13, 2012, 12:00:00

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.


More From » jquery

 Answers
50

NB: This answer applies to dataTables 1.9.x!



For $.fn.dataTableExt.sErrMode the only value there has any importance is alert. It is alert or anything else. sErrMode is handled by the internal dispatcher function _fnLog, in v1.9.2 about line 4575 in media/js/jquery.dataTables.js :



function _fnLog( oSettings, iLevel, sMesg )
{
var sAlert = (oSettings===null) ?
DataTables warning: +sMesg :
DataTables warning (table id = '+oSettings.sTableId+'): +sMesg;

if ( iLevel === 0 )
{
if ( DataTable.ext.sErrMode == 'alert' )
{
alert( sAlert );
}
else
{
throw new Error(sAlert);
}
return;
}
else if ( window.console && console.log )
{
console.log( sAlert );
}
}


Unfortunelaty, there is no way to override dataTables internal functions, believe me - I have tried, not possible with prototyping or anything else. You can read the author Allan Jardines own comment to that here :




I'm sorry to say that due to how DataTables is constructed at the
moment, it's not possible to override an internal function using
Javascript outside of DataTables scope. This is something that will be
addressed whenever I get around to doing the 2.x series (which might
be a while off!) - but at present you would need to alter the core.




One could think that : Hey, perhaps the iLevel-flag can be changed somewhere in the settings? Again, unfortunately no. iLevel is hardcoded in each internal call to _fnLog.



It is somehow disappointing we have to choose between ugly alerts and completely halt of execution, because an error is thrown. A simply override of window.onerror does not work either. The solution is to modify _fnLog, simply comment out the line where the custom error is thrown :



else
{
// throw new Error(sAlert); <-- comment this line
}


And the execution continues if you have $.fn.dataTableExt.sErrMode = 'throw' (anything else but alert) and if errors occurs. Even better, one could need those thrown errors in other situations, set a flag outside, like



window.isDebugging = true;


and



else
{
if (!window.isDebugging) throw new Error(sAlert);
}


This is not a hack in my opinion, but overruling of a general not avoidable jQuery dataTables behaviour that sometimes is not satisfying. As Allan Jardine himself write in the above link :




Why can't you just modify the source? That's the whole point of open
source :-)



[#83666] Sunday, August 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gageherberth

Total Points: 249
Total Questions: 115
Total Answers: 119

Location: Liechtenstein
Member since Sun, Sep 12, 2021
3 Years ago
;