using jQuery and its plugins DataTables, finding a little abnormal behavior; if I have an object as a result, it works quietly; if I have a plain array, however, appears the phrase: "No data available in table".
I found the solution to display the contents of a plain array:
"ajax": {
"url": "./pages/tabledata.php",
"dataSrc": ""
}
but of course in the case of the subject, the script does not work and gives me back the phrase given above.
My question is:
you can change the code attached to check if an object is or if it is a plain array?
Thanks in advance
$(document).ready(function() {
if ($('#mytable').length) {
$('#mytable').dataTable({
"ajax": "./pages/tabledata.php",
"columns": [
{"data": "id", "visible": false, "searchable": false},
{"data": "code"},
{"data": "name"}
],
"order": [ 1, 'asc']
})
}
});
<script src="https://cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table id="mytable" class="display">
<thead>
<tr>
<th>ID</th>
<th>Code of Data</th>
<th>Name User</th>
</tr>
</thead>
</table>
JSON for object:
{
"data":[
{
"id":"1",
"code":"PRF00001",
"name":"Test 1"
},{
"id":"2",
"code":"PRF00002",
"name":"Test 2"
}
]
}
JSON for plain array:
{
"data":{
"id":"1",
"code":"PRFS00001",
"name":"Test 1"
}
}