You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The column sizing feature allows you to optionally specify the width of each column including min and max widths. It also allows you and your users the ability to dynamically change the width of all columns at will, eg. by dragging the column headers.
18
19
20
+
### Column Widths
21
+
19
22
Columns by default are given the following measurement options:
These defaults can be overridden by both `tableOptions.defaultColumn` and individual column defs, in that order.
30
33
34
+
```tsx
35
+
const columns = [
36
+
{
37
+
accessorKey: 'col1',
38
+
size: 270, //set column size for this column
39
+
},
40
+
//...
41
+
]
42
+
43
+
const table =useReactTable({
44
+
//override default column sizing
45
+
defaultColumn: {
46
+
size: 200, //starting column size
47
+
minSize: 50, //enforced during column resizing
48
+
maxSize: 500, //enforced during column resizing
49
+
},
50
+
})
51
+
```
52
+
53
+
The column "sizes" are stored in the table state as numbers, and are usually interpreted as pixel unit values, but you can hook up these column sizing values to your css styles however you see fit.
54
+
31
55
As a headless utility, table logic for column sizing is really only a collection of states that you can apply to your own layouts how you see fit (our example above implements 2 styles of this logic). You can apply these width measurements in a variety of ways:
32
56
33
-
-`table` elements or any elements being displayed in a table css mode
57
+
-semantic `table` elements or any elements being displayed in a table css mode
34
58
-`div/span` elements or any elements being displayed in a non-table css mode
35
59
- Block level elements with strict widths
36
60
- Absolutely positioned elements with strict widths
@@ -39,3 +63,115 @@ As a headless utility, table logic for column sizing is really only a collection
39
63
- Really any layout mechanism that can interpolate cell widths into a table structure.
40
64
41
65
Each of these approaches has its own tradeoffs and limitations which are usually opinions held by a UI/component library or design system, luckily not you 😉.
66
+
67
+
### Column Resizing
68
+
69
+
TanStack Table provides built-in column resizing state and APIs that allow you to easily implement column resizing in your table UI with a variety of options for UX and performance.
70
+
71
+
#### Enable Column Resizing
72
+
73
+
By default, the `column.getCanResize()` API will return `true` by default for all columns, but you can either disable column resizing for all columns with the `enableColumnResizing` table option, or disable column resizing on a per-column basis with the `enableResizing` column option.
74
+
75
+
```tsx
76
+
const columns = [
77
+
{
78
+
accessorKey: 'id',
79
+
enableResizing: false, //disable resizing for just this column
80
+
size: 200, //starting column size
81
+
},
82
+
//...
83
+
]
84
+
```
85
+
86
+
#### Column Resize Mode
87
+
88
+
By default, the column resize mode is set to `"onEnd"`. This means that the `column.getSize()` API will not return the new column size until the user has finished resizing (dragging) the column. Usually a small UI indicator will be displayed while the user is resizing the column.
89
+
90
+
In React TanStack Table adapter, where achieving 60 fps column resizing renders can be difficult, depending on the complexity of your table or web page, the `"onEnd"` column resize mode can be a good default option to avoid stuttering or lagging while the user resizes columns. That is not to say that you cannot achieve 60 fps column resizing renders while using TanStack React Table, but you may have to do some extra memoization or other performance optimizations in order to achieve this.
91
+
92
+
> Advanced column resizing performance tips will be discussed [down below](#advancedcolumnresizingperformance).
93
+
94
+
If you want to change the column resize mode to `"onChange"` for immediate column resizing renders, you can do so with the `columnResizeMode` table option.
95
+
96
+
```tsx
97
+
const table =useReactTable({
98
+
//...
99
+
columnResizeMode: 'onChange', //change column resize mode to "onChange"
100
+
})
101
+
```
102
+
103
+
#### Column Resize Direction
104
+
105
+
By default, TanStack Table assumes that the table markup is laid out in a left-to-right direction. For right-to-left layouts, you may need to change the column resize direction to `"rtl"`.
106
+
107
+
```tsx
108
+
const table =useReactTable({
109
+
//...
110
+
columnResizeDirection: 'rtl', //change column resize direction to "rtl" for certain locales
111
+
})
112
+
```
113
+
114
+
#### Connect Column Resizing APIs to UI
115
+
116
+
There are a few really handy APIs that you can use to hook up your column resizing drag interactions to your UI.
117
+
118
+
##### Column Size APIs
119
+
120
+
To apply the size of a column to the column head cells, data cells, or footer cells, you can use the following APIs:
121
+
122
+
```ts
123
+
header.getSize()
124
+
column.getSize()
125
+
cell.column.getSize()
126
+
```
127
+
128
+
How you apply these size styles to your markup is up to you, but it is pretty common to use either CSS variables or inline styles to apply the column sizes.
129
+
130
+
```tsx
131
+
<th
132
+
key={header.id}
133
+
colSpan={header.colSpan}
134
+
style={{ width: `${header.getSize()}px` }}
135
+
>
136
+
```
137
+
138
+
Though, as discussed in the [advanced column resizing performance section](#advancedcolumnresizingperformance), you may want to consider using CSS variables to apply column sizes to your markup.
139
+
140
+
##### Column Resize APIs
141
+
142
+
TanStack Table provides a pre-built event handler to make your drag interactions easy to implement. These event handlers are just convenience functions that call other internal APIs to update the column sizing state and re-render the table. Use `header.getResizeHandler()` to connect to your column resize drag interactions, for both mouse and touch events.
If you are creating large or complex tables (and using React 😉), you may find that if you do not add proper memoization to your render logic, your users may experience degraded performance while resizing columns.
168
+
169
+
We have created a [performant column resizing example](../examples/react/column-resizing-performant) that demonstrates how to achieve 60 fps column resizing renders with a complex table that may otherwise have slow renders. It is recommended that you just look at that example to see how it is done, but these are the basic things to keep in mind:
170
+
171
+
1. Don't use `column.getSize()` on every header and every data cell. Instead, calculate all column widths once upfront, **memoized**!
172
+
2. Memoize your Table Body while resizing is in progress.
173
+
3. Use CSS variables to communicate column widths to your table cells.
174
+
175
+
If you follow these steps, you should see significant performance improvements while resizing columns.
176
+
177
+
If you are not using React, and are using the Svelte, Vue, or Solid adapters instead, you may not need to worry about this as much, but similar principles apply.
Copy file name to clipboardexpand all lines: docs/guide/row-selection.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -161,7 +161,7 @@ const columns = [
161
161
]
162
162
```
163
163
164
-
#### Connect Row Selection to Row Click Events
164
+
#### Connect Row Selection APIs to UI
165
165
166
166
If you want a simpler row selection UI, you can just hook up click events to the row itself. The `row.getToggleSelectedHandler()` API is also useful for this use case.
The TanStack Table packages do not come with any virtualization APIs or features built-in, but TanStack Table can easily work with other virtualization libraries like [react-window](https://www.npmjs.com/package/react-window) or TanStack's own [TanStack Virtual](https://tanstack.com/virtual/v3)
0 commit comments