Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

461
Visualizações
Jquery datatable add rows

I have a datatable in my page initiated like below:

var dataSet = [
    { "id": 1, "name": "Bennett Weimann", "email": "jtremblay@example.com" },
    { "id": 2, "name": "Bennett Weimann", "email": "buckridge.orin@example.com" }
];

$(document).ready(function () {
    $('#example').DataTable({
        data: dataSet,
        columns: [
            { data: 'id', title: 'Id' },
            { data: 'name', title: 'Name' },
            { data: 'email', title: 'Email' },
        ],
    });
});

Additinally I have a button which makes an ajax post reques, in the answer I get a json

[{"id":1,"name":"Bennett Weimann","email":"jtremblay@example.com"},
{"id":2,"name":"Bennett Weimann","email":"buckridge.orin@example.com"}]

If I try to add the response like below, I get an error

request.done(function (response, textStatus, jqXHR) {

        table = $('#example').DataTable();
        table.clear();
        table.rows.add(response);
        table.draw();

    });

Error is "DataTables warning: table id=example - Requested unknown parameter 'id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4"

However if I copy and paste the response manually, it has no issue whatsoever.

request.done(function (response, textStatus, jqXHR) {

table = $('#example').DataTable();
table.clear();
table.rows.add([
    {"id":1,"name":"Bennett Weimann","email":"jtremblay@example.com"},
    {"id":2,"name":"Bennett Weimann","email":"buckridge.orin@example.com"}
]);
table.draw();

});

Any help appriciated what could cause such error.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I must have been tired.. after a sleep I could see that I was passing an object instead of an array. I am using php as a backend, if I create an array like

    $data = array([
        "id"  => 1,
        "name" => "foo",
        "email" => "bar"
    ]);

    return $data;

and passing the response it works like charm

 request.done(function(response, textStatus, jqXHR) {

        table = $('#example').DataTable();
        table.clear();
        table.rows.add(response);
        table.draw();

    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda