How to load a table using datatables ajax?

2

I'm trying to load a table using the datatables component.

The return of this ajax request is a json object in this format:

data = {
  "CLIENTES": {
    "0": {
      "ID": "1",
      "NOME": 'GABRIEL'
    },
    "1": {
      "ID": "2",
      "NOME": 'RODRIGUES'
    }
  }
}

In the documentation columns data it is mentioned that the following structure should be created:

table.DataTable({
  "ajax": url,
  columns: [
    {"data": "CLIENTES.ID"},
    {"data": "CLIENTES.NOME"}
  ]
});

But access to indexes is not done correctly, in this case they should be accessed:

{"data": "CLIENTES['0'].ID"},
{"data": "CLIENTES['1'].ID"},

Dynamically, how could I do this?

There is a related question that also did not solve my question: Load table with json using datatables

    
asked by anonymous 25.02.2016 / 13:13

1 answer

1

I just recreated the array by removing CUSTOMERS using the $.map function.

Solution:

$(document).ready(function() {
    data = {
      "CLIENTES": {
        "0": {
           "ID": "1",
           "NOME": 'GABRIEL'
        },
       "1": {
           "ID": "2",
           "NOME": 'RODRIGUES'
         }
       }
     };


   var newData = $.map(data.CLIENTES, function(el) { return el });

   $('#example').DataTable({
       data: newData,
       columns: [
       {"data": "ID"},
       {"data": "NOME"}
       ]
    });

});

See working at jsfiddle

    
08.03.2016 / 01:55