Reputation: 742
I'm trying to get dinamically tittles on jquery datatables from object, but it doesn't draw them.
For example, with this object:
var tableData = [
{ id: 1, user: 'admin', pass: 'pass', role: 'admin' },
{ id: 2, user: 'user', pass: 'pass', role: 'user' }
]
this is my method:
function generateDatatable(tableId, tableData) {
var columns = [];
if (tableData.length == 0) {
columns[0] = { data: '-' };
} else {
Object.keys(tableData[0]).forEach(element => {
columns[columns.length] = { data: element };
});
}
jQuery('#' + tableId).DataTable({
data: tableData,
language: dataTablesTexts,
pagingType: 'full',
lengthMenu: [
[10, 25, 50, -1],
[10, 25, 50, 'All']
],
searching: true,
ordering: true,
paging: true,
select: false,
info: true,
responsive: true,
columns: columns
});
}
I can see the values but not the tittles.
Resultant columns will look like this:
var columns = [
{data: "id"},
{data: "user"},
{data: "pass"},
{data: "role"}
]
if I change data: "..."
to title: "..."
I can see the titles but not the values.
What could be wrong with the code.
Upvotes: 2
Views: 236
Reputation:
You should use both: data
to point to specific property within data source that corresponds to current column for each entry and title
to specify column title.
Upvotes: 1