How to make the Dynamic Datatable column

Asked

Viewed 895 times

-2

recently I asked a question:

Error using JSON with datatable

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

1 answer

0


I took advantage of your Fiddler of the other issue and I made an amendment 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 have updated your question with dynamic generation of columns.

  • Hi @Rboscini, thank you for answering, but it was not what I need, there is some way to leave the name of the dynamic column, tipow, only declared ajax, it took the name of the property and set in the column?

  • Of course you do, but I need to know what you want to do, does each column come with the name json? will come in the correct order, will come in each line, how will you send this data via ajax? send the structure we set up together this

  • Let’s take this json as an example: { "Nome": "Felipe3", "Data": "null", "Tipo": "Normal", "RG": "123456798"} my table would automatically have to have the following columns: "Name, Date, Type, RG". That’s the data with your property, you understand? ** will come in the correct order, will come in each line, how you will send this data via ajax? ** A: will come in the correct order, each row is each object, I will not send ajax, just receive

  • changed the Fiddler, take a look.

  • Opa @Rboschini, almost there, only she’s being filled

  • I updated Fiddler, because it gave me -1 ?

  • @Rboscihini, man, n know kkkk I clicked up

Show 3 more comments

Browser other questions tagged

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