DataTable data source JSON Array without AJAX
DataTable data source JSON Array without AJAX
I have successfully created a table with JSON Array data from a SQL database.
I have a file, data.php, that builds the array, which I then refer to with ajax.url.
$('#table').DataTable( {
"ajax": {
"url": "data.php",
"dataSrc": ""
},
My dataset is along the lines of:
[{"Date":"2018-01-19","ID":"4","Start":"2018-01-19 23:06:44"..........}]
For continuity/simplicity with existing pages, I would really like to generate the Array in the same php file as the table. I do this with javascript charts, but for the life of me I cannot get it to work with DataTable. I have tried many variants, including:
var tableData = <?php echo json_encode($jsonArray);?>;
$(document).ready(function() {
$('#table').DataTable( {
"data": tableData
},
I believe the problem lies in that my array is labeled and without a {"data": starting it off. This is what ajax.dataScr="" accomplishes. How can I work around this with an internally generated array?
Thanks for any pointers!
This question has an accepted answers - jump to answer
Answers
The array is an array of objects. You will need to use
columns.data
to define the columns. More information here:https://datatables.net/manual/data/#Objects
Kevin
Thank you for your reply. I have accomplished my goal by putting the JS query data in the <tbody> of my html document.