Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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"
    }
}
share|improve this question
up vote 0 down vote accepted

your problem is that datatables don't recognize the "data" from the plain array format as a array, so there's nothing to list since its a object.

but if u change the format to this:

{
  "data": [ //<=
    {
      "id":"1",
      "code":"PRFS00001",
      "name":"Test 1"
    }
  ] //<=
}

then you will see the data appears again.

i made a fiddle for you here: http://jsfiddle.net/9chxhmk2/

here's a reference to the recognized json format for datatables: http://datatables.net/release-datatables/examples/server_side/post.html

share|improve this answer
    
Thanks kabstergo, your help is always precious! – Gianluigi 'A35G' Feb 6 '15 at 15:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.