Datatable not loading from Ajax/php
Datatable not loading from Ajax/php

I searched, but couldn't find the solution. I am new to Datatable and I am using the latest 20 version. It is a simple dump of all rows from the table with few actions to be performed (CRUD). The data is JSON formatted from PHP, and the SON data is fine (validated with jsoninit), but datable is NOT displaying any row. It is stuck in processing. Please help. Here is my table definition. Please let me know what other info would you need to resolve this issue ? Appreciate your time and help.
var eventData = $('#eventTable').DataTable({
"processing":true,
"ajax":{
url: 'action.php',
type: 'POST',
data: {action:'listEvent'},
dataType: 'json'
},
"columns": [
{ "data": "eventid" },
{ "data": "eventdate" },
{ "data": "eventname" },
{ "data": "totsongs" },
{ "data": "action" },
{ "data": "eventsongs"}
],
"columnDefs":[ {
"targets": "_all",
"orderable": false,
"searchable": false
}],
"lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
"Language": {
"Paginate": {
"First": " ",
"Previous": " ",
"Next": " ",
"Last": " ",
},
"LengthMenu": "Records per page: _MENU_",
"Info": "Total of _TOTAL_ records (showing _START_ to _END_)",
"InfoFiltered": "(filtered from _MAX_ total records)"
}
});
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
Are there any console errors? And, can you post some of the JSON records (say the first 30 lines or so) and the HTML table definition, please - normally these errors are cause due to a mismatch between the JSON, the HTML and the table column declarations.
Colin
Hello Colin, thanks for the reply. I have given below, partial JSON data, HTML code and PHP code.
JSON data (partial), which is validated by jsoninit.
[{"eventid":"1","eventdate":"2020-04-02","eventname":"FIRST EVENTu","totsongs":5,"action":"<button type=\"button\" name=\"update\" id=\"1\" class=\"btn btn-warning btn-xs update\"><span class=\"glyphicon glyphicon-pencil\" data-toggle=\"tooltip\" title=\"Edit this Event\"><\/span><\/button> <button type=\"button\" name=\"copy\" id=\"1\" class=\"btn btn-success btn-xs copy\"><span class=\"glyphicon glyphicon-file\" data-toggle=\"tooltip\" data-original-title=\"Copy this Event\"><\/span><\/button> <button type=\"button\" name=\"song\" id=\"1\" class=\"btn btn-primary btn-xs song\"><span data-toggle=\"tooltip\" data-original-title=\"Add or Update songs for this event\">Select Songs<\/span><\/button> <button type=\"button\" name=\"excel\" id=\"1\" class=\"btn btn-info btn-xs excel\"><span data-toggle=\"tooltip\" data-original-title=\"Download Song list for this event\">Download Song list<\/span><\/button> <button type=\"button\" name=\"delete\" id=\"1\" class=\"btn btn-danger btn-xs delete\"><span class=\"glyphicon glyphicon-trash\" data-toggle=\"tooltip\" data-original-title=\"Delete this Event\"><\/span><\/button> ","eventsongs":"TP000001,TP000002,TP000003,TP000004,TP000005"},{"eventid":"2","eventdate":"2020-04-04","eventname":"April 4th event","totsongs":1,"action":"<button type=\"button\" name=\"update\" id=\"2\" class=\"btn btn-warning btn-xs update\"><span class=\"glyphicon glyphicon-pencil\" data-toggle=\"tooltip\" title=\"Edit this Event\"><\/span><\/button> <button type=\"button\" name=\"copy\" id=\"2\" class=\"btn btn-success btn-xs copy\"><span class=\"glyphicon glyphicon-file\" data-toggle=\"tooltip\" data-original-title=\"Copy this Event\"><\/span><\/button> <button type=\"button\" name=\"song\" id=\"2\" class=\"btn btn-primary btn-xs song\"><span data-toggle=\"tooltip\" data-original-title=\"Add or Update songs for this event\">Select Songs<\/span><\/button> <button type=\"button\" name=\"excel\" id=\"2\" class=\"btn btn-info btn-xs excel\"><span data-toggle=\"tooltip\" data-original-title=\"Download Song list for this event\">Download Song list<\/span><\/button> <button type=\"button\" name=\"delete\" id=\"2\" class=\"btn btn-danger btn-xs delete\"><span class=\"glyphicon glyphicon-trash\" data-toggle=\"tooltip\" data-original-title=\"Delete this Event\"><\/span><\/button> ","eventsongs":"TP000001"},
and here is the HTML that defines the eventTable:
and here is the PHP code that gets the data from table:
Forgot to mention. There is no Console Errors.
Because the data is at the top-level of the returned data, you need to add
""
forajax.dataSrc
, see the middle example on that page.Colin
I added ajax.dataSrc = "", but issue is still the same. But this time I got console error, pasted below:
[Warning] jQuery.Deferred exception: undefined is not an object (evaluating 'n[q].style') (2) (jquery.min.js, line 2)
Ja — datatables.min.js:88:169
ja — datatables.min.js:74
e — datatables.min.js:118:115
(anonymous function) — datatables.min.js:118:200
each — jquery.min.js:2:2578
q — datatables.min.js:108:393
(anonymous function) — datatables.min.js:191:498
(anonymous function) — data.js:6
l — jquery.min.js:2:29381
(anonymous function) — jquery.min.js:2:29679
undefined
[Error] TypeError: undefined is not an object (evaluating 'n[q].style')
(anonymous function) (jquery.min.js:2:31048)
I think we're going to need to see this. Could you link to your page, please, so we can take a look.
Colin
Here is the link to my page.
You have 6 columns defined in Datatables but technically you have only 5 defined in HTML. Here is your HTML:
You have
</th></th>
for the 6th column but it should be<th></th>
.Also you have some duplicate
id
errors that are outside of Datatables but you might want to clean those up.Kevin
I corrected <th> tags but it didn't solve the issue
Did you update the test case? It still looks the same to me.
Kevin
I take back the above stmt. Silly me,correctring <th> tag resolved the issue, after clearing the browser cache. Thanks a lot Kthorngern and Colin, for the wonderful support and help. Appreciate the great prodcut.