initialize datatables using ajax without knowing how many column headers you will have beforehand

initialize datatables using ajax without knowing how many column headers you will have beforehand

dulituldulitul Posts: 2Questions: 2Answers: 0
edited July 2015 in Free community support

I am trying to implement this example http://datatables.net/release-datatables/examples/ajax/objects.html but as you can see the requirement is to pass the column names. as below

"columns": [
{ "data": "name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "extn" },
{ "data": "start_date" },
{ "data": "salary" }
]

However, my table will be dynamic so the column names will change on update so I can't just hardcode some values. I've tried getting the columns names in my ajax response and via 3rd variable pass that to the "columns" requirement as below but it doesn't work. Is there anyway workaround?

       //PAY ATTENTION TO THIS LINE
        var columnsHeaders = [];


  var ab = $('#dynamicScenarioTable').DataTable({
          "bDestroy": true,
          "dom": 'T<"clear">lfrtip',
          "ajax": {
                "type": "POST",
                "url": "dynamicScenario.htm",
                "data": tags,
                "dataType": "json",
                "dataSrc": function(json){
                      //PAY ATTENTION TO THIS LINE
                      columnsHeaders.push({"data": json.header });
                      return json.vals;
                },
                "error": function(exception)
                {
                   displayMessageOnError();
                }
           },  
            //PAY ATTENTION TO THIS LINE
           "columns":  [columnsHeaders],

           tableTools: tableDefaultS.tableTools
  });
This discussion has been closed.