Correctly Suppressing Warnings in DataTables?

前端 未结 6 2126
小蘑菇
小蘑菇 2020-12-24 12:13

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

相关标签:
6条回答
  • 2020-12-24 12:32

    Let me add my 2 cents to davidkonrad's answer above. One way of modifying _fnLog function without changing the file is to get reference to that method from Api instance in datatables settings:

    $.fn.dataTableSettings[0].oApi._fnLog = function(settings, level, msg, tn) {
        // Modified version of _fnLog
    }
    

    Hope that this will be helpful for someone.

    0 讨论(0)
  • 2020-12-24 12:39

    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.

    0 讨论(0)
  • 2020-12-24 12:40

    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);
      }
    }
    
    0 讨论(0)
  • 2020-12-24 12:48

    try this:

    $.fn.DataTable.ext.oApi._fnLog = function (settings, level, msg, tn) {
        msg = 'DataTables warning: ' +
                (settings !== null ? 'table id=' + settings.sTableId + ' - ' : '') + msg;
    
        if (tn) {
            msg += '. For more information about this error, please see ' +
                    'http://datatables.net/tn/' + tn;
        }
        console.log( msg );
    };
    
    0 讨论(0)
  • 2020-12-24 12:51

    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 :-)

    0 讨论(0)
  • 2020-12-24 12:58

    As of DataTables version 1.10.15, you can set $.fn.dataTableExt.errMode to 'ignore' and it will silently ignore the error messages:

        $(document).ready(function () {
            $.fn.dataTableExt.errMode = 'ignore';
        });
    

    _fnLog DataTables function has the following code :

            if ( type == 'alert' ) {
                alert( msg );
            }
            else if ( type == 'throw' ) {
                throw new Error(msg);
            }
            else if ( typeof type == 'function' ) {
                type( settings, tn, msg );
            }
    

    The default value is 'alert' which is problematic.

    You can also set to 'throw'. It will create javascript error, but will not do disturb the user.

    'ignore' or any other values will just sliently skip the error.

    0 讨论(0)
提交回复
热议问题