Data not getting displayed for dynamic columns
Data not getting displayed for dynamic columns
Just a snippet of the code with hard coded data for testing since ajax version was doing the same thing. Even with hard coded data, data is not displayed, only the column names.
if ($.fn.DataTable.isDataTable('#cons-pd-datatable'))
{
$('#cons-pd-datatable').DataTable().destroy();
}
var columns = [];
columns.push({
data: "prod_name",
title: "Product Name"
})
for (let i = 1; i <= 5; i++) {
columns.push({
data: 'day_' + i,
title: 'Day ' + i
});
}
console.log(columns)
let cons_data = {
prod_name: 'Hub',
day_1: 10,
day_2: 7,
day_3: 22,
day_4: 30,
day_5: 15,
}
console.log(cons_data)
$('#cons-pd-datatable').DataTable({
"data": cons_data,
"columns": columns
});
Code is shown above. In the data table, I can see the column names, but the data is not displayed. Just shows "No data available in table". Am I missing something?
Answers
Never mind. Found the solution. cons_data needs to be an array.
Thanks for the update. Yes the value of
data
must be an array.Allan