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.
You can define this during datatable initialization
Hide columns dynamically
The previous answers are using legacy DataTables syntax. In v 1.10+, you can use column().visible():
To hide multiple columns, columns().visible() can be used:
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:
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:If use data from json and use Datatable v 1.10.19, you can do this:
More info
For anyone using server-side processing and passing database values into jQuery using a hidden column, I suggest "sClass" param. You'll be able to use css display: none to hide the column while still being able to retrieve its value.
css:
In datatables init:
//EDIT: remember to add your hidden class to your thead cell also
You can try as below to hide/show dynamically runtime
Hide : fnSetColumnVis( 1, false, false );
Example: oTable.fnSetColumnVis(item, false,false);
Show : fnSetColumnVis( 1, true, false );
Example: oTable.fnSetColumnVis(item, false,false);
Here, oTable is object of Datatable.
if anyone gets in here again this worked for me...