Datatable with JSON

Asked

Viewed 2,164 times

2

I’m making a datatable with ajax return.

$.getJSON("sql.php?operacao=usuario_lista&excluidos=false", function (data) {
    alert(data.users);

    $('#tabusuarios').DataTable({
        "aaData": data.users,
        "aoColumns": [
            { "mDataProp": "ID" },
            { "mDataProp": "NOME" },
            { "mDataProp": "USUARIO" }
        ]
    });
});

My JSON returns to:

{
    "users": [
        {
            "ID": "89",
            "NOME": "aaabbbb",
            "USUARIO": "a.b",
            "DTNASC": "2017-01-01",
            "NUM": "112233"
        },
        {
            "ID": "76",
            "NOME": "Bruno Pereira",
            "USUARIO": "brunopereira",
            "DTNASC": "2011-01-01",
            "NUM": "11"
        }
    ]
}

But he gives the error:

Datatables Warning: table id=tabusuarios - Cannot reinitialise Datatable. For more information about this error, Please see http://datatables.net/tn/3

And don’t populate a table..

  • Have you tried creating your table based on the examples of datatables.net? There are other ways to read a json, see here (ajax custom_data_property).

  • already tried yes Florida.... it just does not carry anything...

1 answer

2


In accordance with reply on Soen, you can do it this way:

$(document).ready(function() {
    $('#tabusuarios').DataTable({
        "processing" : true,
        "ajax" : {
            "url" : "sql.php?operacao=usuario_lista&excluidos=false",
            dataSrc : ''
        },
        "columns" : [ {
            "data" : "ID"
        }, {
            "data" : "NOME"
        }, {
            "data" : "USUARIO"
        }]
    });
});
  • I did as you put...now not errors, but the table is like "No data available in table", and the ajax returned data

  • but I saw now that in what they mentioned above, with using "https://api.myjson.com/bins/14t4g" it will... It seems that there must be some problem with the way I’m returning my json...

  • The result of your JSON is with the content-type right? Or is getting back a TXT?

  • Marcelo I found out what eh.. in the return of my sql.php I did: echo '{"users":'. json_encode($result). '}'; ... When I took this part of the users and returned directly json_encode($result) it was....

  • 1

    But I’m using your code too... Thank you to Voce and everyone who answered! Valews!!!! :)

  • I would comment on exactly this. Try to change the column of data for data.users

  • but then in your example I would put where the data.users? in the Columns part?

  • that, try something like "data.users" : "ID"

  • I got it. Thank you very much!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.