Dynamic columns load from php ajax array
Dynamic columns load from php ajax array
Hi Support,
I am using datatable along ajax request to PHP and it returns json encoded data. This return data contains array of json objects.
I am using below code to load the datatable and it's currently working fine.
$(document).ready(function(){
var table = $('#data').DataTable( {
"stateSave": true,
"serverSide": true,
ajax: {
url: url here,
type: POST,
data: function (d) {
//input parameters here
}
},
"drawCallback": function( settings ) {
//some code is here
},
autoWidth: false,
columnDefs: [
{ "className": "FISCAL_YEAR dt-center", "targets": [ 0 ] },
{ "className": "OCTOBER dt-center", "targets": [ 0 ] },
{ "className": "NOVEMBER dt-center", "targets": [ 0 ] },
{ "className": "DECEMBER dt-center", "targets": [ 0 ] },
//rest of the columndefs here
],
columns: [
{ data: "FISCAL_YEAR", "width": "5%" },
{ data: "OCTOBER", "width": "5%" },
{ data: "NOVEMBER", "width": "5%" },
{ data: "DECEMBER", "width": "5%" },
//rest of the columns here
],
});
});
Here is my json data.
{
"draw":1,
"recordsTotal":0,
"recordsFiltered":4,
"data":[
{
"July":"0.00",
"August":"0.00",
"September":"0.00",
"October":"0.00",
"November":"0.00",
"December":"0.00",
"January":"0.00",
"February":"0.00",
"March":"0.00",
"April":"0.00",
"May":"0.00",
"June":"0.00",
"REVISED_BUDGET_AMOUNT":"0",
"ADOPTED_BUDGET_AMOUNT":"0",
"BEGINNING_BALANCE":"0",
"FISCAL_YEAR":"2019"
}
]
}
Now I want to load the column names dynamically instead of hard coded column names from the above json object returned by ajax call.
Please help.
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
This example from this thread should get you going, it's demonstrating just that,
Colin