How to make the Dynamic DataTable column

-2

I recently asked a question:

Error when using JSON with datatable

What I need is similar, but I need to leave the name of the dynamic column, that is, I would have some way to get the property of my json and leave it set as the name of my column, thank you

    
asked by anonymous 04.01.2016 / 16:53

1 answer

0

I took your fiddler from the other question and made a change based on that new question.

$(document).ready(function() {

  var json = '[ {  "Nome": "Felipe",  "Data": "null",  "Tipo": "Normal",  "RG": "123456798"}, {  "Nome": "Felipe2", "Data": "null", "Tipo": "Normal", "RG": "123456798"}, { "Nome": "Felipe3", "Data": "null", "Tipo": "Normal", "RG": "123456798"}]';

  var column_names = []; //Array com confi das colunas
  var jsonObj = JSON.parse(json); //Parse no obj json, facilita o manuseio

  //Verifica se tem registro, para nao caga tudo
  if(jsonObj.length>0){

    //Monta array de colunas
      $.each(Object.keys(jsonObj[0]),function(idx,obj){
      column_names.push({"sTitle": obj, "mData":obj});
    });

    $('.table').DataTable({
      "aaData": jsonObj,
      "aoColumns":column_names
    });

  }

});

See if it helps you

I updated your question with dynamic column generation.

    
04.01.2016 / 17:00