Hide multiple columns based on condition
Hide multiple columns based on condition

I am using a drawCallback function, trying to hide the columns with empty header name.
"drawCallback": function ( settings ) {
$('#contracts thead tr th').each(function() {
var head = $(this).text();
console.log(head);
if (head == "") {
table.columns().visible(false);
}
else {
table.columns().visible( true );
}
});
I tried using multiple logics but nothing worked out. Whenhead=" "
it tends to hide all columns, but I just want to hide columns with empty column header names
Your help will be highly appreciated
Thank you
This discussion has been closed.
Answers
You will probably only want to do this once, not for each table draw, in the
initComplete
callback.Using
columns()
without acolumn-selector
will result in selecting all the columns. I suggest you usecolumns().every()
to loop through each column. In the loop usecolumn().header()
to get the header. Here is an example of what you want:http://live.datatables.net/kiqibemi/1/edit
Kevin
@kthorngren
Thank you!
Because the table heading change every time the value is selected in dropdown menu (this is just a requirement) so it has to redraw with each selection.
You example works perfect , I just put it inside
drawcallback function`` like:
This is how I approache dit earlier
Thank you for your help! As always, appreciate it