正确抑制数据表中的警告? [英] Correctly Suppressing Warnings in DataTables?

查看:20
本文介绍了正确抑制数据表中的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试正确抑制数据表中的警告(警报).DataTables 的标准行为是在发生错误时抛出 javascript 警报;但是,目前这对我来说很不方便.我一直在尝试将警告转换为 javascript 错误

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';

哪个工作正常,但这会停止当前的 javascript 执行,这不是我想要的.因此,我将 DataTables 操作(初始化和更改)包装在 try-catch 中,没有错误处理;但是,这也会停止 javascript 执行.(在 Chrome 和 Firefox 上测试)

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.

推荐答案

注意:此答案适用于 dataTables 1.9.x!

对于$.fn.dataTableExt.sErrMode,唯一重要的值是警报".它是警报"或其他任何东西.sErrMode 由内部调度函数 _fnLog 处理,在 v1.9.2 中关于 media/js/jquery.dataTables.js 的第 4575 行:

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 );
    }
}

不幸的是,没有办法覆盖 dataTables 的内部函数,相信我 - 我已经尝试过,原型设计或其他任何东西都不可能.您可以在此处阅读作者艾伦·贾丁斯 (Allan Jardines) 对此的评论强>:

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 :

我很遗憾地说,由于 DataTables 在时刻,不可能使用覆盖内部函数DataTables 范围之外的 Javascript.这将是每当我开始做 2.x 系列(这可能稍等片刻!)-但目前您需要更改核心.

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.

人们可能会认为:嘿,也许 iLevel 标志可以在设置中的某处更改?再次,不幸的是没有.iLevel 被硬编码在每个对 _fnLog 的内部调用中.

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.

我们不得不在丑陋的警报和完全停止执行之间做出选择,这有点令人失望,因为抛出了错误.简单地覆盖 window.onerror 也不起作用.解决办法是修改_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
}

如果您有 $.fn.dataTableExt.sErrMode = 'throw'(除了alert"之外的任何其他内容)并且发生错误,则继续执行.更好的是,在其他情况下可能需要那些抛出的错误,在外面设置一个标志,比如

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;

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

在我看来,这不是黑客",而是推翻了有时不令人满意的一般无法避免的 jQuery dataTables 行为.正如艾伦·贾丁 (Allan Jardine) 本人在上述链接中所写:

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

这篇关于正确抑制数据表中的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆