ShakibDShy

Column Visibility Example

Learn how to toggle and control the visibility of table columns using React Table Grid

This example demonstrates how to control which columns are visible in your table. You can allow users to toggle columns on and off to focus on the most important data.

Overview

In this example, you will learn how to:

  • Define columns with type safety using createColumnHelper
  • Set initial visibility of columns
  • Toggle the visibility of individual columns
  • Track state changes when columns are shown or hidden

Example

Column Visibility

ID
Name
Age
Email
Department
Role
Salary
Status
Location
Phone
Join Date
1
John Doe
28
john.doe@company.com
Engineering
Software Engineer
$85,000
Active
New York
+1 (555) 123-4567
1/15/2023
10
Jennifer Martinez
36
jennifer.m@company.com
Engineering
DevOps Engineer
$98,000
Active
San Diego
+1 (555) 012-3456
2/28/2022
11
William Clark
42
william.c@company.com
Finance
Senior Accountant
$86,000
Active
Phoenix
+1 (555) 123-4567
5/17/2021
12
Emma Rodriguez
30
emma.r@company.com
Marketing
Digital Marketing
$72,000
Active
Las Vegas
+1 (555) 234-5678
3/8/2023
13
Daniel White
35
daniel.w@company.com
Engineering
QA Engineer
$80,000
Active
Minneapolis
+1 (555) 345-6789
9/12/2022
14
Sophia Lee
32
sophia.l@company.com
HR
HR Specialist
$70,000
Active
Detroit
+1 (555) 456-7890
1/20/2023
15
Christopher Moore
38
chris.m@company.com
Sales
Sales Manager
$95,000
Active
Atlanta
+1 (555) 567-8901
11/5/2021
16
Olivia Garcia
29
olivia.g@company.com
Engineering
UI Designer
$76,000
Active
Houston
+1 (555) 678-9012
7/30/2022
17
Andrew Wilson
44
andrew.w@company.com
Finance
Finance Director
$120,000
Active
Philadelphia
+1 (555) 789-0123
12/15/2020
18
Isabella Thompson
31
isabella.t@company.com
Marketing
Brand Manager
$84,000
Active
San Jose
+1 (555) 890-1234
4/25/2022
19
Matthew Brown
36
matthew.b@company.com
Engineering
System Architect
$105,000
Active
Dallas
+1 (555) 901-2345
8/9/2021
2
Jane Smith
34
jane.smith@company.com
Marketing
Marketing Manager
$92,000
Inactive
San Francisco
+1 (555) 234-5678
8/20/2022
20
Ava Martinez
33
ava.m@company.com
Sales
Sales Representative
$73,000
Active
Sacramento
+1 (555) 012-3456
10/17/2022
21
Ethan Anderson
40
ethan.a@company.com
Engineering
Lead Developer
$110,000
Active
New York
+1 (555) 123-4567
2/14/2021
22
Mia Johnson
34
mia.j@company.com
HR
Recruitment Manager
$82,000
Active
San Francisco
+1 (555) 234-5678
5/30/2022
23
Alexander Davis
37
alex.d@company.com
Finance
Financial Controller
$98,000
Active
Chicago
+1 (555) 345-6789
12/7/2021
24
Charlotte Wilson
31
charlotte.w@company.com
Marketing
SEO Specialist
$71,000
Active
Boston
+1 (555) 456-7890
2/11/2023
25
Benjamin Taylor
39
ben.t@company.com
Engineering
Mobile Developer
$88,000
Active
Seattle
+1 (555) 567-8901
1/19/2022
26
Amelia Moore
35
amelia.m@company.com
Sales
Business Developer
$79,000
Active
Austin
+1 (555) 678-9012
8/5/2022
27
Henry Garcia
43
henry.g@company.com
Finance
Risk Analyst
$94,000
Active
Miami
+1 (555) 789-0123
6/22/2021
28
Victoria Lee
32
victoria.l@company.com
HR
Training Coordinator
$68,000
Active
Portland
+1 (555) 890-1234
1/9/2023
29
Sebastian Clark
36
sebastian.c@company.com
Engineering
Data Engineer
$96,000
Active
Denver
+1 (555) 901-2345
3/14/2022
3
Robert Johnson
41
robert.j@company.com
Sales
Sales Director
$115,000
pending
Chicago
+1 (555) 345-6789
3/10/2021
30
Scarlett White
33
scarlett.w@company.com
Marketing
Social Media Manager
$74,000
Active
San Diego
+1 (555) 012-3456
11/28/2022
31
Jack Thompson
41
jack.t@company.com
Sales
Key Account Manager
$89,000
Active
Phoenix
+1 (555) 123-4567
9/3/2021
32
Luna Rodriguez
30
luna.r@company.com
Engineering
UX Designer
$77,000
Active
Las Vegas
+1 (555) 234-5678
4/17/2023
33
Owen Martinez
38
owen.m@company.com
Finance
Budget Analyst
$83,000
Active
Minneapolis
+1 (555) 345-6789
2/8/2022
34
Elena Anderson
34
elena.a@company.com
HR
Benefits Coordinator
$69,000
Active
Detroit
+1 (555) 456-7890
10/11/2022
35
Lucas Brown
37
lucas.b@company.com
Engineering
Security Engineer
$99,000
Active
Atlanta
+1 (555) 567-8901
7/26/2021
36
Aria Davis
31
aria.d@company.com
Marketing
Product Marketing
$76,000
Active
Houston
+1 (555) 678-9012
12/19/2022
37
Leo Wilson
42
leo.w@company.com
Sales
Sales Operations
$87,000
Active
Philadelphia
+1 (555) 789-0123
4/5/2021
38
Zoe Garcia
35
zoe.g@company.com
Engineering
Cloud Engineer
$97,000
Active
San Jose
+1 (555) 890-1234
6/30/2022
39
Max Lee
36
max.l@company.com
Finance
Investment Analyst
$91,000
Active
Dallas
+1 (555) 901-2345
9/15/2022
4
Emily Davis
29
emily.d@company.com
Engineering
Frontend Developer
$78,000
Active
Boston
+1 (555) 456-7890
4/5/2023
40
Lily Moore
33
lily.m@company.com
HR
HR Business Partner
$81,000
Active
Sacramento
+1 (555) 012-3456
1/23/2023
41
Noah Clark
39
noah.c@company.com
Engineering
Technical Lead
$108,000
Active
New York
+1 (555) 123-4567
10/12/2021
42
Aurora White
32
aurora.w@company.com
Marketing
Marketing Analyst
$73,000
Active
San Francisco
+1 (555) 234-5678
3/28/2023
43
Elijah Thompson
40
elijah.t@company.com
Sales
Enterprise Sales
$112,000
Active
Chicago
+1 (555) 345-6789
5/31/2021
44
Maya Rodriguez
34
maya.r@company.com
Engineering
ML Engineer
$103,000
Active
Boston
+1 (555) 456-7890
8/22/2022
45
Gabriel Martinez
37
gabriel.m@company.com
Finance
Tax Specialist
$85,000
Active
Seattle
+1 (555) 567-8901
1/10/2022
46
Ruby Anderson
31
ruby.a@company.com
HR
Talent Acquisition
$72,000
Active
Austin
+1 (555) 678-9012
2/6/2023
47
Adrian Brown
38
adrian.b@company.com
Engineering
DevOps Manager
$106,000
Active
Miami
+1 (555) 789-0123
11/15/2021
48
Stella Davis
35
stella.d@company.com
Marketing
Events Manager
$78,000
Active
Portland
+1 (555) 890-1234
7/11/2022
49
Felix Wilson
41
felix.w@company.com
Sales
Channel Partner Manager
$93,000
Active
Denver
+1 (555) 901-2345
8/30/2021
5
Michael Wilson
45
michael.w@company.com
Finance
Financial Analyst
$95,000
pending
Miami
+1 (555) 567-8901
11/30/2020
50
Hazel Garcia
33
hazel.g@company.com
Engineering
Full Stack Developer
$90,000
Active
San Diego
+1 (555) 012-3456
4/4/2022
6
Sarah Brown
31
sarah.b@company.com
HR
HR Manager
$88,000
Inactive
Seattle
+1 (555) 678-9012
6/15/2022
7
David Lee
37
david.l@company.com
Engineering
Backend Developer
$92,000
Active
Austin
+1 (555) 789-0123
9/22/2021
8
Lisa Anderson
33
lisa.a@company.com
Marketing
Content Strategist
$75,000
Active
Portland
+1 (555) 890-1234
12/1/2022
9
James Taylor
39
james.t@company.com
Sales
Account Executive
$82,000
Active
Denver
+1 (555) 901-2345
7/14/2021

Key Concepts

  • Visibility Toggle: The visibleColumns state controls which columns are displayed.
  • Interactive Controls: Buttons allow users to individually show or hide columns.
  • State Management: Use of onStateChange to synchronize external state with the table.

Customization Tips

  • Add styling to the toggle buttons for better UI feedback.
  • Use conditional rendering to customize which columns are shown.

Best Practices

  1. UX: Offer a clear and simple UI for toggling column visibility.
  2. Performance: Avoid excessive updates by debouncing state changes if necessary.
  3. Accessibility: Ensure buttons have proper ARIA labels for screen readers.

Next Steps

Last updated on

On this page