Dynamic Columns in DataTables
Dynamic Columns in DataTables
After much searching I was finally able to get dynamic columns to load correctly.
Here is the magic line of code:
[code]
$('#mc_table').dataTable().fnSettings().nTHead.innerHTML = ''; // This is the line that makes it work.
$('#mc_table').dataTable( {
"bDestroy": true, // This makes the table check for new data every time.
"aaData": _data(),
"aoColumns": _cols(),
"bPaginate": false,
"bJQueryUI": false,
"bLengthChange" : true,
"bFilter": false,
"bSort": true,
"bInfo": false,
"bAutoWidth": false,
"aaSorting": [[3,'desc'],[4,'asc']]
} );
[/code]
This line will remove the current header for the table forcing dataTable to rebuild the header with the new columns in the column object.
I put that line directly in front of my dataTable initializer.
Here is the magic line of code:
[code]
$('#mc_table').dataTable().fnSettings().nTHead.innerHTML = ''; // This is the line that makes it work.
$('#mc_table').dataTable( {
"bDestroy": true, // This makes the table check for new data every time.
"aaData": _data(),
"aoColumns": _cols(),
"bPaginate": false,
"bJQueryUI": false,
"bLengthChange" : true,
"bFilter": false,
"bSort": true,
"bInfo": false,
"bAutoWidth": false,
"aaSorting": [[3,'desc'],[4,'asc']]
} );
[/code]
This line will remove the current header for the table forcing dataTable to rebuild the header with the new columns in the column object.
I put that line directly in front of my dataTable initializer.
This discussion has been closed.
Replies
The code is not working. This is the error message I get "DataTables warning (table id = 'example'): Unexpected number of TD elements. Expected 5 and got 1. DataTables does not support rowspan / colspan in the table body, and there must be one cell for each row/column combination."