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: 9Questions: 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
This question has an accepted answers - jump to answer
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
Hi Allan,
Thank you very much for your help. It is pretty incredible how you provide all of this for free, and even answer questions at 9pm on a Saturday night!
That did the trick - thank you.
If I could be cheeky to ask - that does now work - but there are a couple of issues:
When I change the colour selection using the drop-down select menu at the top of the page, the content from the
https://api.color.pizza/v1/API is appended to the existing rows.Each time the selection changes, the number of colours from returned by the API when the selection changes, is added to
Nin theShowing 1 to 10 of N entriessection.I did try changing the
populateTableRowsfunction to go from this:But that results in an empty table... I also tried:
But that also results in an empty table.
Thanks again for your help
Jim
What I can I say - you spelt "colour" correctly
You've got exactly the right idea with
clear(), just calling it in the wrong place. If you do the clear, then add the rows, and finally draw the table, that will do what you are looking for: https://live.datatables.net/butafeho/4/edit .Allan
Haha - yes - I did nearly use "color" when taking the original code from this Codepen but it jarred too much and had to go with the UK spelling.
Thank you again Allan for your help and support and for providing such an incredible product. All sorted now
Jim