ShakibDShy

Column Resizing Example

Learn how to enable and configure column resizing using React Table Grid

This example shows you how to implement column resizing in your table. With column resizing enabled, users can drag and adjust the width of table columns, making it easier to view data that varies in length.

Overview

In this example, we demonstrate:

  • How to define columns with type safety using createColumnHelper
  • How to enable column resizing by setting the enableColumnResize prop
  • Handling state changes to monitor column resizing events
  • Configuring the direction of the column resizing (left-to-right or right-to-left) using a hook

Example

Column Resizing Table

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

Key Concepts

Enabling Resizing

To allow users to resize columns, set the enableColumnResize prop to true:

<TableGrid enableColumnResize // other props />

Handling State Changes

The onStateChange callback lets you track changes in table state, including column resizing events. This can be useful for debugging or saving user preferences.

onStateChange={(state) => {
  console.log("Table state changed:", state);
}}

Configuring Resizing Direction

The columnResizeDirection prop accepts either ltr (left-to-right) or rtl (right-to-left). This example uses a custom hook (useDirection) to dynamically provide the direction.

columnResizeDirection={direction as "ltr" | "rtl"}

Customization Tips

  • Styling: Use the styleConfig prop to customize how the table, headers, rows, and cells look during resizing.
  • Performance: For large datasets, consider performance optimizations such as memoization.

Best Practices

  1. Type Safety: Always define your data structures using TypeScript interfaces and use createColumnHelper for consistency.
  2. User Feedback: Consider providing visual indicators (like a resize handle) to show when a column is being resized.
  3. Accessibility: Ensure that column resizing interactions are keyboard accessible and provide appropriate ARIA labels where needed.

Next Steps

Last updated on

On this page