loading table from AJAX

loading table from AJAX

robDanielrobDaniel Posts: 3Questions: 2Answers: 0
edited May 2016 in Free community support

My HTML looks like this :

  <table id='userTable' class='table table-striped table-bordered' >
        <thead>
        <tr><th>Column One</th><th>Column Two</th><th>Column Three</th></tr>
        </thead>
        <tbody></tbody>
        </table>

I initialise dataTables like this :

    $('#userTable').DataTable({
           "ajax": {
                "url": "data.json"
            },
         });

data.json looks like this :

{
   "data": ["columnOne","second column","3rd col"  ]
}

But my table looks like this on the screen :

Column One  Column Two  Column Three
     3                      r                   d
     c                          o                    l
     s                          e                    c

ie - 1 Character from each string per column

What am I doing wrong ?

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • robDanielrobDaniel Posts: 3Questions: 2Answers: 0

    My data.json was wrong.

    Needs to be :

    {
       "data": [
       ["columnOne","second column","3rd col"]
         ]
    }
    

    Note the extra pair of [ ] brackets

  • allanallan Posts: 61,920Questions: 1Answers: 10,153 Site admin

    That is correct - thanks for posting back. The data parameter should be an array of arrays or an array of objects (i.e. one entry for each row in the table).

    Allan

This discussion has been closed.