javascript 数据表:未捕获的类型错误:无法读取未定义的属性“parentNode”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16903031/
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
Datatables: Uncaught TypeError: Cannot read property 'parentNode' of undefined
提问by ComputerLocus
I'm getting two errors in my console upon viewing a webpage. Datatables is being used, and I am quite sure (as the errors state) that the issue is related to Datatables. The problem here is that I did not write this code, I am merely now trying to fix issues in the code. The odd thing is that this section of the code should have been working before, as I can visibly see it working on the live version. Am I correct in what I believe to be the error? If so, what is the best way to go about debugging this, when the code is not made by me, and the code and data is in large quantities?
查看网页时,我的控制台出现两个错误。正在使用数据表,我很确定(如错误所述)该问题与数据表有关。这里的问题是我没有写这段代码,我只是现在试图修复代码中的问题。奇怪的是,这部分代码以前应该可以运行,因为我可以明显地看到它在实时版本上运行。我认为错误是正确的吗?如果是这样,当代码不是我制作的,并且代码和数据量很大时,最好的调试方法是什么?
DataTables warning: Unexpected number of TD elements. Expected 2040 and got 1981. DataTables does not support rowspan / colspan in the table body, and there must be one cell for each row/column combination. jquery.dataTables.js:5840
Uncaught TypeError: Cannot read property 'parentNode' of undefined. jquery.dataTables.js:2843
数据表警告:TD 元素的数量意外。预计2040,得到1981。DataTables不支持表格主体中的rowspan/colspan,并且每个行/列组合必须有一个单元格。jquery.dataTables.js:5840
未捕获的类型错误:无法读取未定义的属性“parentNode”。jquery.dataTables.js:2843
回答by Richard
Do you have a URL that you can post so that we can take a look?
你有一个可以发布的网址,以便我们可以查看吗?
Cannot read property
parentNodeof undefined
is a javascript error that happens because you are trying to get the parentNode
of something that doesn't exist.
Cannot read property
parentNodeof undefined
是一个 javascript 错误,因为您试图获取parentNode
不存在的东西。
The other error seems quite obvious? The number of TD elements in a TR is not in line with the rest of the table. Look at the HTML and check if the number of TD & TH elements in the THEAD, TFOOT and TBODY are all the same count. "DataTables does not support rowspan / colspan in the table body"
另一个错误似乎很明显?TR 中 TD 元素的数量与表格的其余部分不一致。查看 HTML 并检查 THEAD、TFOOT 和 TBODY 中 TD 和 TH 元素的数量是否都相同。“DataTables 在表体中不支持 rowspan / colspan”
Please post a URL or HTML/JS snippet of the table, preferrably on http://www.jsfiddle.net
请发布表格的 URL 或 HTML/JS 片段,最好在http://www.jsfiddle.net
回答by ParPar
I had this problem and I solved it by empty the table before any refresh or refill:
我遇到了这个问题,我通过在任何刷新或重新填充之前清空表来解决它:
$('#table').empty();
$('#table').dataTable(...);
回答by maricavor
I had this problem and I solved it by adding sDom:
我遇到了这个问题,我通过添加 sDom 解决了它:
sDom: '<"top"><"clear">rt<"bottom"><"clear">'
Before it was:
之前是:
sDom: ''
回答by Aditya Gupta
I had the same error but different solution resolved it.
I had already set the property destroy : true
while initializing the datatable. This helps in re-initializing the table.
Issue :
In datatable you have to provide a property 'columns': columnArray,
while initializing.
It looks something like:
我有同样的错误,但不同的解决方案解决了它。
我已经destroy : true
在初始化数据表时设置了属性。这有助于重新初始化表。
问题:
在数据表中,您必须'columns': columnArray,
在初始化时提供一个属性。它看起来像:
$('#example').dataTable( {
"ajaxSource": "sources/objects.txt",
"columns": [
{ "data": "engine" },
{ "data": "browser" },
{ "data": "platform" },
{ "data": "version" },
{ "data": "grade" }
],
destroy: true
} );
This columnArray
has to be populated based on ajax data. I was populating this for every ajax call, thus bloating up the array with multiple entries for the same columns. I fixed it by re-initializing the array to []
.
这columnArray
必须根据 ajax 数据进行填充。我正在为每个 ajax 调用填充它,从而使数组膨胀到同一列的多个条目。我通过将数组重新初始化为[]
.
回答by Simon
Unrelated to this specific case, but might come in useful for anyone else searching for this issue.
与此特定案例无关,但可能对搜索此问题的其他人有用。
I had the same error and it turned out to be caused by specifying a sort column that didn't exist. I specified column 11, but the table only had 10 columns.
我有同样的错误,结果证明是由指定不存在的排序列引起的。我指定了第 11 列,但该表只有 10 列。