Using datatables with json
Using datatables with json
dyetube
Posts: 20Questions: 5Answers: 0
I'm trying to populate a table with data from a .json file but for some reason datatables says "No data available in table". I can hand code the data and it shows up just fine but as soon as I load it (through jquery) into the table from a .json document, I get nothing. I have the datatable init script in the
<body>
tag of my html and the
$.getJSON
function in the
<head>
tag of my html. What am I doing wrong here?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Not sure why that isn't working - can you link to a test case showing the issue please.
Allan
I created a version you can run locally: http://www.filedropper.com/datatablereport Run this and let me know what you see is the problem.
Thank you - the issue is almost certainly an async one.
Your first block of code sets up an Ajax request to get the data. Then the DataTable is created, and then the Ajax response is received and the data is appended to the table. Since the table has already been created, DataTables doesn't 'see' the data you append using jQuery (you'd need to use
row.add()
.You need to remove this async behaviour. Either move the JSON load into the document ready function and initialise the DataTable after the table has been populated, or just let DataTables do the Ajax request using the
ajax
option.Allan
Yep. Hate when I do a simple mistake like that. Thanks a lot for the answer. Works perfectly now.