admin管理员组文章数量:1345897
I am trying to make the table that I am creating on the fly with JavaScript
into a .dataTable
. I have pared my code to my co-workers and it is almost identical. I have made the necessary changes, but I keep getting the JavaScript
error: "Cannot read property 'parentNode' of null" in the console.
All of the code is executing perfectly. The new table is being displayed in the browser, but once it tries to run the line $('#data').dataTable({"paging": false});
it gives me the error.
Can anyone see what is causing this? Maybe a scope error, or a parentheses/bracket in wrong spot.
function Load(p1, p2) {
var work= "";
$('#div1').empty();
var tablearea = document.getElementById('div1'),
table = document.createElement('table');
table.id = "data";
table.style.fontSize = "small";
table.width = "100%";
var tbody = document.createElement('tbody');
tbody.id = "dataTB";
table.appendChild(tbody);
tablearea.appendChild(table);
var url = sessionStorage.baseUrl + "XXXXXXXXX";
$.getJSON(url)
.done(function (data) {
var results = $.parseJSON(data);
if (results != null)
{
for (i = 0; i < results.length; i++) {
work += "<tr><td>" + results.data[i].info + "</td></tr>";
}
}
$('#dataTB').html(work);
$('#data').dataTable({"paging": false});
})
.fail(function (jqXHR, textStatus, err) {
alert('Error: ' + err);
});
}
I am trying to make the table that I am creating on the fly with JavaScript
into a .dataTable
. I have pared my code to my co-workers and it is almost identical. I have made the necessary changes, but I keep getting the JavaScript
error: "Cannot read property 'parentNode' of null" in the console.
All of the code is executing perfectly. The new table is being displayed in the browser, but once it tries to run the line $('#data').dataTable({"paging": false});
it gives me the error.
Can anyone see what is causing this? Maybe a scope error, or a parentheses/bracket in wrong spot.
function Load(p1, p2) {
var work= "";
$('#div1').empty();
var tablearea = document.getElementById('div1'),
table = document.createElement('table');
table.id = "data";
table.style.fontSize = "small";
table.width = "100%";
var tbody = document.createElement('tbody');
tbody.id = "dataTB";
table.appendChild(tbody);
tablearea.appendChild(table);
var url = sessionStorage.baseUrl + "XXXXXXXXX";
$.getJSON(url)
.done(function (data) {
var results = $.parseJSON(data);
if (results != null)
{
for (i = 0; i < results.length; i++) {
work += "<tr><td>" + results.data[i].info + "</td></tr>";
}
}
$('#dataTB').html(work);
$('#data').dataTable({"paging": false});
})
.fail(function (jqXHR, textStatus, err) {
alert('Error: ' + err);
});
}
Share
Improve this question
edited Jun 14, 2016 at 5:27
Roger
1,6312 gold badges30 silver badges57 bronze badges
asked May 15, 2015 at 17:16
webminer07webminer07
3134 gold badges8 silver badges25 bronze badges
4
-
2
I feel like we're not seeing everything . . . where is
table
defined? Where is the error occuring, according to the console (because I don't see any reference toparentNode
in your code)? – talemyn Commented May 15, 2015 at 17:22 - Well I define the table on the fifth line with var tablearea = document.getElementById('div1'), table = document.createElement('table'); table.id = "data"; table.style.fontSize = "small"; table.width = "100%"; but this is a team project made up of tons of files, but the parentNode error seems like a mon error not particular to a specific parentNode variable – webminer07 Commented May 15, 2015 at 17:26
-
Well,
parentNode
isn't a variable, it's a property of the JSNode
object ( developer.mozilla/en-US/docs/Web/API/Node/parentNode ) that represents the parent of a current element node. Based on the error, somewhere in the logic fordataTable
it is trying to find theparentNode
of a non-existant element. My bet is that it, you are calling$('#data').dataTable({"paging": false});
before thedata
table element has finished being added to the DOM, so it can't find it. – talemyn Commented May 15, 2015 at 17:48 - I've thought about this, but I've stepped through step by step in the Chrome debugger, and watched the table with id='data' get added. And then subsequently everything after get added to the table. – webminer07 Commented May 15, 2015 at 18:01
4 Answers
Reset to default 4I think you must include a valid thead
element in your table
I had this problem when using DataTables.js
directory and I solved it by empty the table before any refresh or refill:
$('#data').empty();
I would not append the table to the DOM until the data rows were added to the table. So I would try something like the following instead (Inside the JSON callback) :
var tbody = document.createElement('tbody');
tbody.id = "dataTB";
table.appendChild(tbody);
var tablearea = document.getElementById('div1'),
table = document.createElement('table');
table.id = "data";
table.style.fontSize = "small";
table.width = "100%";
for (i = 0; i < results.length; i++) {
$(tbody).append("<tr><td>" + results.data[i].info + "</td></tr>");
}
tablearea.appendChild(table);
Here's a workin jsbin: http://jsbin./vacabe/1/
I had the same situation with this library. I'm using it with Knockout.JS and to make it work i added at the end of my viewModel a timeout:
setTimeout(() => {
// Call the get function at load
GetFiltersData();
}, 0);
This will not affect you page and i tested this with several browsers with success.
版权声明:本文标题:Adding dataTable to HTML table with JavaScript: Cannot read property 'parentNode' of null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743818038a2544284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论