How to set data source from an object?

How to set data source from an object?

huytuduyhuytuduy Posts: 2Questions: 1Answers: 0

I try to set data of datatable with an object as code below but my datatable is still empty. So, can I pass an object to data source?
$('#example').dataTable( {
data: {rows: [{c: "xxx", d: "eee"}, {c: "xxx", d: "eee"}, {c: "xxx", d: "eee"}, {c: "xxx", d: "eee"}]},
"sAjaxDataProp": "rows",
"aoColumns": [
{ "mDataProp": "c" },
{ "mDataProp": "d" }
]
} );

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,880Questions: 1Answers: 10,139 Site admin
    Answer ✓

    data doesn't take an object - it takes an array. Just pass in your array of data.

    Allan

  • huytuduyhuytuduy Posts: 2Questions: 1Answers: 0

    Thank you for your quick answer. I made it work.
    var oData = {rows: [{c: "xxx", d: "eee"}, {c: "xxx", d: "eee"}, {c: "xxx", d: "eee"}, {c: "xxx", d: "eee"}]};
    $('#example').dataTable( {
    data: oData.rows,
    "columns": [
    { "data": "c" },
    { "data": "d" }
    ]
    } );

This discussion has been closed.