Buttons not displaying and generating warning 'cannot reinitialize DataTable'
Buttons not displaying and generating warning 'cannot reinitialize DataTable'
I'd like to preface this discussion by saying I did refer to the troubleshooting page at 'http://datatables.net/tn/3' and that didn't help. As for DataTable code, I have all of the includes required for functionality. The reference to DataTables in my code is in one and ONLY one spot, so the reinitialise issue isn't due to my making multiple calls to the dataTable function as suggested in the troubleshooting page. I'm at a loss as to why the buttons aren't showing and generating an error. Of note the dataTable functionality works perfectly UNTIL I add in the button features, per the provided code samples.
javascript:
function xmlTableParser(proxyURL, tableID) {
var request = new XMLHttpRequest();
request.open("GET", proxyURL, true);
request.send();
request.onreadystatechange = function() {
if ((this.readyState === 4) && (this.status === 200)) {
// Convert the XML document to a string
xmlString = this.responseText.toString();
// Eliminate the Meta-Query from the <Query> tag in the XML results as it can potentially cause issues when reading the XML code
var badQuery = xmlString.substring((parseInt(xmlString.search("Resource"))-1), (parseInt(xmlString.search('Result'))-5));
xmlString = xmlString.replace(badQuery, '');
// Convert string of XML code back into an XML document
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlString,"text/xml");
// Collect the number of rows (<Tuple> tags) and columns (<Answer> tags per <Tuple> tag) of data from the XML document
var tupleCount = xmlDoc.getElementsByTagName("Tuple").length;
var columnCount = xmlDoc.getElementsByTagName("Answer").length / tupleCount;
// Build the HTML Table's contents from the data in the XML document
var rowHTML = "";
for (var i = 0; i < tupleCount; i++) {
rowHTML = '<tr>';
for (var j = 0; j < columnCount; j++) {
if (j == 0 || j == 4) {}
else {
rowHTML += '<td nowrap>' + xmlDoc.getElementsByTagName("Answer")[((columnCount*i)+j)].childNodes[0].nodeValue + '</td>';
}
}
**_ rowHTML += '</tr>';
$(tableID).append(rowHTML);
$(document).ready( function () {
$('#myTable').dataTable({
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
]
});
}_**);
}
}}}
And the table itself:
<div style=" margin-top:8px; z-index:0;" id="mainDiv">
<table id="myTable" style="border:1px solid lightgrey;">
<th>header 1</th>
<th>header 2</th>
<th>header 3</th>
<th>header 4</th>
<th>header 5</th>
<th>header 6</th>
<th>header 7</th>
<th>header 8</th>
</table>
</div>
EDIT: Updated using Markdown Code formatting.
Replies
Wow, the formatting of trying to post this discussion got all kinds of hosed. Made worse by the fact there doesn't appear to be an edit function on the post to try and fix it.
You need to use Markdown code formatting. There is a link to the docs below the
Preview
button when you are creating a post.I think the default edit time is 10 or 15 minutes. After that edit is disabled.
In line 20 of the above code you have:
for (var i = 0; i < tupleCount; i++) {
It looks like this code is within this for loop:
If thats the case then multiple attempts at initializing the Datatable are made. Also you probably don't want to use
$(document).ready( function ()
inside a function.Note that in line 29 you have
$(tableID).append(rowHTML);
but are using$('#myTable').dataTable({
with hard coded table ID. Do you want to use thetableID
instead?Kevin
Thanks for the advice- first time user of your forums.
The for loop is for the purpose of taking dynamic data from the server and generating a table row for each data set. Datatable is inside the loop in order to capture each table row generated, and throwing that to the table utilizing the dataTable code. I've tried to store the captured data from the server to an array and then outputting the array to the dataTable, but I couldn't get the sort,search, pagination, etc features to work when trying that.
The Datatable should be initialized only after all the HTML has been built and appended to the page. In this case you are using DOM sourced data as described here:
https://datatables.net/manual/data/#Data-sources
This typically means that you are getting a Javascript error that is stopping the script. Take a look at the browser's console for the errors.
You can load the data either way. If you can it would be helpful to have a link to your page or a test case replicating the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Ok, I managed to move the dataTable function out of the loop and still have dataTables work properly, however the buttons still aren't showing (at least I'm not getting the initialization error anymore). Checking the console, I got the following TypeErrors:
TypeError p is undefined
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js:8
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js:8
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js:8
TypeError l is undefined
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/buttons.flash.min.js:30
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/buttons.flash.min.js:8
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/buttons.flash.min.js:8
TypeError v is undefined
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js:11
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js:8
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js:8
TypeError k is undefined
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js:6
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js:5
<anonymous> https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js:5
Caught my own mistake, problem above resolved. The console is clean of errors, however the buttons still aren't rendering.
Your Datatables init code looks ok. Sounds like you aren't loading all the correct JS and CSS files or they are in the wrong order. Please post a link to your page or a test case replicating the issue so we can take a look.
Kevin
Here are all JS and CSS files being used, in order:
jquery.js should precede DataTables' references.
Also, why do you have jquery.dataTables.min.js and datatables.js ?
Awesome, that was the problem!
As soon as I removed reference to datatables.js everything worked! Woohoo!
Thanks guys
Interesting side effect of getting the export buttons to work- the results set drop down box goes away. Is it possible to have both the drop down (10, 25, 50,100 results) AND the export button options?
Yes you can do that, please see this FAQ:
https://datatables.net/faqs/index#buttons-page-length
Kevin