javascript JQuery DataTables 和 ColVis 插件错误“无法读取未定义的属性‘sWidth’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20673825/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:00:37  来源:igfitidea点击:

Error with JQuery DataTables and ColVis plugin "Cannot read property 'sWidth' of undefined"

javascriptjquerydatatables

提问by Scott Terry

Using the jquery.datatables plugin with the ColVis addon, I recieve this error when I remove a column:

使用带有 ColVis 插件的 jquery.datatables 插件,当我删除列时收到此错误:

"Cannot read property 'sWidth' of undefined". I haven't been able to find a solution to this error online.

“无法读取未定义的属性‘sWidth’”。我一直无法在网上找到解决此错误的方法。

I'm not sure what's causing the error, although I do have a fix that I would like to post for the benefit of other's who run into this issue.

我不确定是什么导致了错误,尽管我确实有一个修复程序,我想发布以供遇到此问题的其他人使用。

I'm using jquery.datatables 1.9.4 from http://datatables.net/.

我正在使用来自http://datatables.net/ 的jquery.datatables 1.9.4 。

回答by user1398619

I had this error when the number of columns in

当列数在

<thead></thead>

was different from the number of columns in

与列数不同

$('#ls-table').DataTable($.extend({}, window.coonDataTableOptions, {
    columns: [
         <here>
    ]
 }));

回答by Scott Terry

On line 3255 of the DataTables source code is this line of code:

DataTables源代码的第3255行是这行代码:

nThs[i].style.width = o.aoColumns[iVis].sWidth;

In this case o.aoColumns[iVis] is null because the column represented by the index has just been hidden. It seems like I've run into a corner case that the creators of the plugins weren't expecting. The above code gets called in response to an internal datatables event, which is triggered by a method called by ColVis when a column is hidden. All that's needed to work around this is to change the above code to:

在这种情况下,o.aoColumns[iVis] 为空,因为索引表示的列刚刚被隐藏。我似乎遇到了插件的创建者没有预料到的极端情况。上面的代码被调用以响应内部数据表事件,该事件由隐藏列时由 ColVis 调用的方法触发。解决这个问题所需要做的就是将上面的代码更改为:

var column = o.aoColumns[iVis];

if(column != null) {
    nThs[i].style.width = o.aoColumns[iVis].sWidth;
}

unfortunately this requires editing the core plugin code, but I'll put in a bug report and hope that they resolve this soon. in the meantime, hopefully this helps people looking for a workaround.

不幸的是,这需要编辑核心插件代码,但我会提交错误报告并希望他们尽快解决这个问题。同时,希望这可以帮助人们寻找解决方法。

回答by Samrendra

Such error occurs just due to populating columns within

发生此类错误只是由于在其中填充列

...DataTable(... "columns":... )

...DataTable(..."列":...)

mismatch with the defined HTML page Have number of columns.

与定义的 HTML 页面不匹配 列数。

.. ..

……

回答by Danico Balane

use $("thead").empty();after the mainTable.clear().destroy().draw();

使用$("thead").empty();mainTable.clear().destroy().draw();

the problem here on data table; once the data from jquery loaded to html5 "thead" the data table jquery eg. mainTable.clear().destroy().draw();fails to delete the data on thead so the solution is to delete it using jquery .empty().

数据表上的问题;一旦来自 jquery 的数据加载到 html5 “thead” 数据表 jquery 例如。mainTable.clear().destroy().draw();无法删除 thead 上的数据,因此解决方案是使用 jquery 删除它.empty()

Your welcome :)

别客气 :)

回答by wjy

search a.aoColumns[D].sWidth,replace

搜索 a.aoColumns[D].sWidth,替换

 var column = a.aoColumns[D];if(column != null) {c.style.width=a.aoColumns[D].sWidth}