Datatables 3.1.5 - data populates, but data disappears when interact with table
Datatables 3.1.5 - data populates, but data disappears when interact with table
u20241229
Posts: 7Questions: 4Answers: 0
Problem
I am working on this test case:
https://live.datatables.net/butafeho/1/edit
Debugger
I can't get the debugger to work, sorry - it comes up with a message:
DataTables library not found!
Event though the start of the javascript is:
new DataTable('#colours');
window.DataTable = DataTable;
Disclaimer
I am a bit of a hobbyist so apologies for glaring mistakes.
Issue
When the page first loads, the datatable seems to initialise okay, sort of.
In the bottom-right it says:
Showing 0 to 0 of 0 entries
And clicking any column headings to sort data clears the table of any records.
I'm sorry for the basic question - I can't see what I have done wrong?
Thanks
Answers
Hi,
You are running into this FAQ:
You aren't using
appendChild(), but it boils down to the same thing. Specifically, you are initialising the DataTable and then changing the DOM from under it.DataTables doesn't watch the DOM for
.innerHTMLchanges made externally, so at the moment when you initialise the DataTable on#coloursit has no data in it. Then you runpopulateTableRowsand it uses.innerHTMLto write the DOM rows in. But DataTables doesn't know that has happened!You could delay initialisation until the data has been written, but
row.add()is perhaps a better option here.And your DataTable init becomes:
i.e. use
row.add()to add objects of the data, and let DataTables create the DOM for you.https://live.datatables.net/butafeho/3/edit
Allan