1

I am attempting to use the jquery datatables plugin to populate a html table via a JSON object returned from a PHP script. I have a bit of confusion concerning naming of the json object and options in the datatables plugin. Passing back a json object like so:

echo json_encode(array('data'=>$res));

yields:

Uncaught TypeError: Cannot read property 'length' of undefined 

while changing data to aaData successfully populates the table, however without headers.

Several fourm posts mentioned the json object must be returned as aaData, but the datatables example documentation on their website gives an example json object named with data.

Here is my current client side code, I am relatively certain that my server side is correct.

$('document').ready(function(){
    $('#result').dataTable({
        "ajax": {
            'url' : 'ppmAlarms.php',

        },

        "columns": [
            { "data": "location" },
            {"data" : "descAndType" },
            {"data" : "timestamp"}
        ]
    });

    });

JSON structure is as follows:

{
  "aaData": [
    {
      "location": "loc1",
      "descAndType": "desc1",
      "timestamp": "2014-08-20 11:34:10"
    }
]
}

1 Answer 1

2

Just realized I just have to specify my own column headers in thead, they aren't populated:

<thead>
      <tr>
         <th>Name</th>
         <th>Position</th>
         <th>Office</th>
         <th>Extn.</th>
         <th>Start date</th>
         <th>Salary</th>
     </tr>
</thead>
Sign up to request clarification or add additional context in comments.

2 Comments

I was scratching my head as well.
But how to add headers dynamically from data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.