columns().visible()
Get / set the visibility of the selected columns.
Description
Showing and hiding columns in a DataTable can be quite handy, particularly when showing tables with a large information density. This method allows the visibility of columns to be changed on-the-fly, or the visibility state of the columns to be read.
Types
function columns().visible()
- Description:
Get the visibility of the selected columns.
- Returns:
API instance with the result set containing a boolean value for each column the selector matched. The boolean values indicate:
true
if the column is visible,false
if it is not.
function columns().visible( show [, redrawCalculations ] )
- Description:
Set the visibility of the selected columns.
- Parameters:
Name Type Optional 1 show
No Specify if the columns should be visible (
true
) or not (false
).2 redrawCalculations
Yes - default:true Indicate if DataTables should recalculate the column layout (
true
- default) or not (false
). Typically this would be left as the default value, but it can be useful to disable when using the method in a loop - so the calculations are performed on every call as they can hamper performance.- Returns:
DataTables API instance with selected columns in the result set.
Examples
Set column visibility for two columns:
var table = new DataTable('#myTable');
// Hide two columns
table.columns([1, 2]).visible(false);
alert(
"Table's column visibility are set to: " +
table
.columns()
.visible()
.join(', ')
);
Hide all columns with a class of '.detail':
var table = new DataTable('#myTable');
table.columns('.detail').visible(false);
Hide multiple columns using redrawCalculations
to improve performance:
var table = new DataTable('#myTable');
table.columns([0, 1, 2, 3]).visible(false, false);
table.columns.adjust().draw(false); // adjust column sizing and redraw
Related
The following options are directly related and may also be useful in your application development.