javascript 正确抑制数据表中的警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11941876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correctly Suppressing Warnings in DataTables?
提问by jab
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
我正在尝试正确抑制数据表中的警告(警报)。DataTables 的标准行为是在发生错误时抛出 javascript 警报;但是,目前这对我来说很不方便。我一直在尝试通过以下方式将警告转换为 javascript 错误
$.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)
哪个工作正常,但这会停止当前的 javascript 执行,这不是我想要的。因此,我将 DataTables 操作(初始化和更改)包装在 try-catch 中,没有错误处理;但是,这也会停止 javascript 执行。(在 Chrome 和 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.
我的问题是如何摆脱这些错误/警报以进行调试?我正在尝试调试脚本的其他部分,但这些警报一直在妨碍我。
采纳答案by davidkonrad
NB: This answer applies to dataTables 1.9.x!
注意:此答案适用于 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
:
因为$.fn.dataTableExt.sErrMode
唯一重要的价值是“警报”。它是“警报”或其他任何东西。sErrMode
由内部调度程序函数处理_fnLog
,在 v1.9.2 关于第 4575 行中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:
不幸的是,没有办法覆盖 dataTables 的内部函数,相信我 - 我已经尝试过,原型设计或其他任何东西都不可能。您可以在此处阅读作者 Allan Jardines 对此的评论:
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.
我很遗憾地说,由于目前 DataTables 的构造方式,不可能在 DataTables 范围之外使用 Javascript 覆盖内部函数。每当我开始做 2.x 系列时都会解决这个问题(这可能需要一段时间!) - 但目前您需要更改核心。
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
.
有人可能会想:嘿,也许 iLevel 标志可以在设置中的某处更改?再次,不幸的是没有。iLevel
在对 的每个内部调用中进行硬编码_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 :
我们不得不在丑陋的警报和完全停止执行之间做出选择,这在某种程度上令人失望,因为抛出了错误。简单的覆盖window.onerror
也不起作用。解决方案是修改_fnLog
,只需注释掉抛出自定义错误的行:
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
如果您有$.fn.dataTableExt.sErrMode = 'throw'
(除“警报”之外的任何其他内容)并且发生错误,则继续执行。更好的是,在其他情况下可能需要那些抛出的错误,在外面设置一个标志,比如
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 :
在我看来,这不是“黑客”,而是对有时不令人满意的一般无法避免的 jQuery dataTables 行为的否决。正如艾伦·贾丁 (Allan Jardine) 本人在上述链接中所写:
Why can't you just modify the source? That's the whole point of open source :-)
为什么不能直接修改源码?这就是开源的全部意义:-)
回答by orad
I modified the native alert using this closure function to redirect DataTables warnings to the console.
我使用此关闭函数修改了本机警报,以将 DataTables 警告重定向到控制台。
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.
它window.alert
在第一次触发时将 恢复到其本机功能。如果您不希望它恢复到原始警报,只需注释掉该window.alert = nativeAlert;
行即可。
回答by Taylor
Here's a solution proposed herethat's slightly modified and works in v1.10.2 without having to change any vendor files:
这是此处提出的一个解决方案,该解决方案稍作修改,可在 v1.10.2 中运行,而无需更改任何供应商文件:
$.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);
}
}
回答by Recev Yildiz
As of DataTables version 1.10.15, you can set $.fn.dataTableExt.errModeto 'ignore' and it will silently ignore the error messages:
从 DataTables 版本1.10.15 开始,您可以将$.fn.dataTableExt.errMode设置为“ ignore”,它会默默地忽略错误消息:
$(document).ready(function () {
$.fn.dataTableExt.errMode = 'ignore';
});
_fnLogDataTables function has the following code :
_fnLogDataTables 函数具有以下代码:
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.
默认值为“ alert”,这是有问题的。
You can also set to 'throw'. It will create javascript error, but will not do disturb the user.
你也可以设置为' throw'。它会产生 javascript 错误,但不会打扰用户。
'ignore' or any other values will just sliently skip the error.
' ignore' 或任何其他值只会悄悄地跳过错误。
回答by Felipe Elias Balarin
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 );
};
回答by Azat
Let me add my 2 cents to davidkonrad's answerabove.
One way of modifying _fnLog
function without changing the file is to get reference to that method from Api instance in datatables settings:
让我将我的 2 美分加到上面davidkonrad 的答案中。在_fnLog
不更改文件的情况下修改函数的一种方法是从数据表设置中的 Api 实例中获取对该方法的引用:
$.fn.dataTableSettings[0].oApi._fnLog = function(settings, level, msg, tn) {
// Modified version of _fnLog
}
Hope that this will be helpful for someone.
希望这对某人有帮助。