Data not getting displayed for dynamic columns

Data not getting displayed for dynamic columns

gdbhatgdbhat Posts: 2Questions: 1Answers: 0

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

  • gdbhatgdbhat Posts: 2Questions: 1Answers: 0

    Never mind. Found the solution. cons_data needs to be an array.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Thanks for the update. Yes the value of data must be an array.

    Allan

Sign In or Register to comment.