Comprehensive Guide to Hiding and Showing Columns in jQuery DataTables

Nov 24, 2025 · Programming · 8 views · 7.8

Keywords: jQuery DataTables | Column Hiding | column().visible() | fnSetColumnVis | Table Control

Abstract: This article provides an in-depth exploration of various methods for dynamically hiding and showing table columns in jQuery DataTables. It focuses on the recommended column().visible() API method in DataTables 1.10+, while comparing it with the traditional fnSetColumnVis() function. The paper details configuration options for hiding columns during initialization, including the use of columns and columnDefs parameters, and demonstrates implementation scenarios through practical code examples. Additionally, it discusses the practical application value of hidden columns in data filtering and server-side processing.

Overview of DataTables Column Visibility Control

jQuery DataTables, as a powerful table plugin, offers flexible mechanisms for controlling column visibility. In practical development, dynamically hiding or showing specific columns based on different view requirements is a common usage scenario. DataTables supports multiple approaches to achieve this functionality, from initialization configuration to runtime dynamic adjustments, meeting various business needs.

API Methods for Dynamic Column Hiding

In DataTables 1.10+ versions, the recommended approach is using the column().visible() API method to dynamically control column visibility. This method accepts a boolean parameter to set the display state of columns. Here's a basic usage example:

var table = $('#example').DataTable();
// Hide the first column
table.column(0).visible(false);
// Show the first column
table.column(0).visible(true);

For scenarios requiring simultaneous operation on multiple columns, the columns().visible() method can be used:

var table = $('#example').DataTable();
// Hide both second and third columns simultaneously
table.columns([1, 2]).visible(false);

Traditional fnSetColumnVis Method

In earlier versions of DataTables, the primary method for controlling column visibility was the fnSetColumnVis() function. While not the current recommended practice, understanding this approach is essential for maintaining legacy code:

// Hide column with index 1
fnSetColumnVis(1, false);
// Show column with index 1
fnSetColumnVis(1, true);

The first parameter of this method is the column index (starting from 0), and the second parameter is the visibility state (true for visible, false for hidden).

Configuring Hidden Columns During Initialization

Beyond runtime dynamic adjustments, DataTables also supports configuring hidden columns directly during initialization. This can be achieved through the columns option:

$('#example').DataTable({
    columns: [
        null, // First column visible
        { visible: false }, // Second column hidden
        null, // Third column visible
        { visible: false } // Fourth column hidden
    ]
});

Alternatively, using the more flexible columnDefs option allows for more precise targeting of columns:

$('#example').DataTable({
    columnDefs: [
        { 
            visible: false, 
            targets: [1, 3] // Hide second and fourth columns
        }
    ]
});

Data Processing Characteristics of Hidden Columns

It's important to note that even when columns are hidden, they remain part of the table data. This means:

The following example demonstrates how to control searchability while hiding columns:

$('#example').DataTable({
    columnDefs: [
        {
            targets: 2,
            visible: false,
            searchable: false // Column not searchable
        },
        {
            targets: 3,
            visible: false,
            searchable: true // Column remains searchable
        }
    ]
});

CSS-Based Hiding Solutions

Beyond using DataTables' built-in API, column display can also be controlled through CSS classes. This approach may offer advantages in certain specific scenarios:

/* CSS definition */
.hidden-column {
    display: none;
}

// DataTables configuration
$('#example').DataTable({
    columnDefs: [
        { 
            className: 'hidden-column', 
            targets: [0] // Apply hidden class to first column
        }
    ]
});

The advantage of this method lies in leveraging CSS cascading properties, though it's important to note that DataTables may not fully recognize this hiding approach.

Analysis of Practical Application Scenarios

In real-world projects, column hiding functionality is commonly used in the following scenarios:

  1. View Switching: Display different column combinations based on user roles or view modes
  2. Data Simplification: Hide secondary information on mobile devices or in space-constrained environments
  3. Data Processing: Hide columns used for calculations or identification without affecting data integrity
  4. Progressive Enhancement: Provide additional detail columns on top of basic views

Performance Considerations and Best Practices

When using column hiding functionality, the following performance factors should be considered:

Compatibility and Version Considerations

Different versions of DataTables have variations in column hiding functionality:

By appropriately applying these column hiding techniques, developers can create more flexible and user-friendly data table interfaces that meet data presentation requirements across different scenarios.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.