Daniel Sh.
Daniel Sh.

Reputation: 2074

"No data available in table" in DataTables

I get the message when trying to populate my table. Here's the simplified code.

$("#showUsers").on("click", function() {
    $.ajax({
        url: "/showUsers",
        contentType: "application/json",
        processData: false,
        complete: function(data) {
            $("#output").html(data.responseText);
            $("#example").dataTable({
                "aaData": data,
                "aoColumns": [{
                    "sTitle": "Name",
                    "mDataProp": "name",
                    "sDefaultContent": ""
                }, {
                    "sTitle": "Movie",
                    "mDataProp": "movie",
                    "sDefaultContent": ""
                }]
            });
        }
    });
});

And

<table id="example">
    <thead>
        <th>name</th>
        <th>movie</th>
    </thead>
</table>

The data is assigned valid json:

[{
    "name": "Dan",
    "movie": "Amelie"
}, {
    "name": "Rob",
    "movie": "Dungeon"
}]

I've read that sometimes it messes with the table itself and the headers and stuff so I tried removing the <thead> from my table definition but it results with the same issue.

Funny thing is that, if I replace "aaData":data with "aaData": plus the json itself, it works. But I can't make data fill the requirements needed by DT.

The ajax data comes from a node-mysql module which is working properly.

If I change data to data.responseText (this is the json as shown before) I get the warning requested unknown parameter 'name' from the data source for row 0.

I know this should be fairly simple for any not-so experienced DataTables user so thanks in advance. Somehow I'm being unable to sort it out.


SOLVED

The MySQL response comes with a data.responseJSON which is a working format to give to aaData. Thanks.

Upvotes: 3

Views: 10762

Answers (2)

lolesque
lolesque

Reputation: 12000

Just got wondering why my dataTable was not filling.

In addition to the correct json format needed with aaData, iTotalRecords and iTotalDisplayRecord, aaData needs to be an array (['a', 'b', ...]) and not an object ({'a': 'b', ...}).

Upvotes: 0

Daniel Sh.
Daniel Sh.

Reputation: 2074

For the sake of flagging the question as awnsered.

SOLVED.

The mysql response comes with a data.responseJSON which is a working format to give to aaData. Thanks.

Upvotes: 1

Related Questions