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

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

问题描述

我试图正确地抑制DataTables中的警告(警报)。 DataTables的标准行为是发生错误时发出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操作(init和changes)包装在一个没有错误处理的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.

推荐答案

注意:此答案适用于数据表1.9.x!

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

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自己的评论,该 here

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 dataTable行为有时不令人满意。正如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天全站免登陆