Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery datatables hide column

Is there a way with the jquery datatables plugin to hide (and show) a table column?

I figured out how to reload the table data: using fnClearTable and fnAddData.

But my issue is that in one of my views for the table (e.g. a hidden mode) I don't want to show certain columns.

like image 731
john Avatar asked Apr 13 '11 19:04

john


People also ask

How to hide a column in jquery DataTable?

Hide columns dynamically var dt = $('#example'). DataTable(); //hide the first column dt. column(0). visible(false);

How to hide column in jquery DataTable based on condition?

To hide and show columns use columns() and visible() method. Call it on dataTables instance and pass column index in columns() method and false to visible() method.

How do you collapse a column in DataTable?

Add and remove columns using "j"; Collapse columns using "j" and "by".


1 Answers

Hide columns dynamically

The previous answers are using legacy DataTables syntax. In v 1.10+, you can use column().visible():

var dt = $('#example').DataTable(); //hide the first column dt.column(0).visible(false); 

To hide multiple columns, columns().visible() can be used:

var dt = $('#example').DataTable(); //hide the second and third columns dt.columns([1,2]).visible(false); 

Here is a Fiddle Demo.

Hide columns when the table is initialized

To hide columns when the table is initialized, you can use the columns option:

$('#example').DataTable( {     'columns' : [         null,         //hide the second column         {'visible' : false },         null,         //hide the fourth column         {'visible' : false }     ] }); 

For the above method, you need to specify null for columns that should remain visible and have no other column options specified. Or, you can use columnDefs to target a specific column:

$('#example').DataTable( {     'columnDefs' : [         //hide the second & fourth column         { 'visible': false, 'targets': [1,3] }     ] }); 
like image 161
devlin carnate Avatar answered Sep 24 '22 08:09

devlin carnate