1

For some reason, my data is not getting populated in the table.

I have a JSON data and all I want is to fill the datatable.

Can someone help me and let me know which simple step am I missing ?

I have version 1.9.4.

<table id="example">
    <thead>
        <tr><th>name</th>
            <th>position</th>
            <th>salary</th>
            <th>start_date</th>
            <th>office</th>
            <th>extn</th>        
    </tr>
    </thead>
    <tbody></tbody>
</table>

$('#example').dataTable({
        data: [
            [
                "Tiger Nixon",
                "System Architect",
                "$3,120",
                "2011/04/25",
                "Edinburgh",
                "5421"
            ],
            [
                "Garrett Winters",
                "Director",
                "5300",
                "2011/07/25",
                "Edinburgh",
                "8422"
            ]
        ]

    });

fiddle

2 Answers 2

3

Here is a 1.9.4 example. You'll need to

  1. pass a JSON with objects, not an array
  2. target aaData, not data or aoData
  3. specify aoColumns, eg column -> json value

here I'm only using the first two "columns" of your data above :

var json = [
    { "name" : "Tiger Nixon", "position" : "System Architect" /*,.,.,.*/ },
    { "name" : "Garrett Winters", "position" : "Director"  /*,.,.,.*/ }
];

var table = $('#example').dataTable({
     aaData : json,
     aoColumns: [
        { mDataProp: "name" },
        { mDataProp: "position" }
    ]
}); 

fiddle -> http://jsfiddle.net/4e7myzmm/
the same in dataTables 1.10.x -> http://jsfiddle.net/c27jj9he/

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of data: , you need to use aaData:

$('#example').dataTable({
    aaData: [
      [...

By default DataTables will use the "aaData" property of the returned data which is an array of arrays with one entry for each column in the table. See docs

jsfiddle

Comments

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.