I am retrieving a dataset that will include different lengths of rows via the Datatables ajax request. For example, one response might be:
... ['jan', 'feb', 'march', 'april'] ...
where as a different response would only be:
... ['jan', 'feb'] ...
Most examples that I have seen have the table headers already hard-coded into the HTML. What I want to do is to dynamically update the headers and their respective column data.
I was hoping to be able to do something like this where I am able to update the headers with dataSrc, but by that time the datatable has already been created and fails to fill in the rest of the table correctly.
....
table = $('#datatables').DataTable( {
"ajax": {
"url": "/foo/bar/",
"type": "GET",
dataSrc: function (json) {
populate_table_header(json)
return json.blah;
}
},
...
I am perfectly ok with destroying and re-creating the table, I just need to know how to create the table while using the AJAX response data for new headers.
Edit
Here's an example
{
"data":[
...
[
"8290808-123123",
"Boo Far",
"[email protected]",
"Other",
"12",
"21390",
"123",
"-",
"-"
],
[
"123123-032489",
"Foo Bar",
"[email protected]",
"Name",
"1",
"2",
"50",
"-",
"-"
],
...
],
"header":[
"Owner",
"Subscription",
"Oct '16",
"Nov '16",
"Dec '16",
"Jan '17",
"Feb '17"
],
}
Is this possible without making a separate AJAX call before initializing the datatable?
columnsbefore populating the table with data. Does that make sense?