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
By default, columns are ordered in the order they are defined in the `columns` array. However, you can manually specify the column order using the `columnOrder` state. Other features like column pinning and grouping can also affect the column order.
19
+
20
+
### What Affects Column Order
21
+
17
22
There are 3 table features that can reorder columns, which happen in the following order:
18
23
19
24
1.[Column Pinning](../guide/column-pinning) - If pinning, columns are split into left, center (unpinned), and right pinned columns.
20
25
2. Manual **Column Ordering** - A manually specified column order is applied.
21
26
3.[Grouping](../guide/grouping) - If grouping is enabled, a grouping state is active, and `tableOptions.columnGroupingMode` is set to `'reorder' | 'remove'`, then the grouped columns are reordered to the start of the column flow.
27
+
28
+
> **Note:**`columnOrder` state will only affect unpinned rows if used in conjunction with column pinning.
29
+
30
+
### Column Order State
31
+
32
+
If you don't provide a `columnOrder` state, TanStack Table will just use the order of the columns in the `columns` array. However, you can provide an array of string column ids to the `columnOrder` state to specify the order of the columns.
33
+
34
+
#### Default Column Order
35
+
36
+
If all you need to do is specify the initial column order, you can just specify the `columnOrder` state in the `initialState` table option.
> **Note:** If you are using the `state` table option to also specify the `columnOrder` state, the `initialState` will have no effect. Only specify particular states in either `initialState` or `state`, not both.
49
+
50
+
#### Managing Column Order State
51
+
52
+
If you need to dynamically change the column order, or set the column order after the table has been initialized, you can manage the `columnOrder` state just like any other table state.
53
+
54
+
```jsx
55
+
const [columnOrder, setColumnOrder] = useState<string[]>(['columnId1', 'columnId2', 'columnId3']); //optionally initialize the column order
56
+
//...
57
+
consttable=useReactTable({
58
+
//...
59
+
state: {
60
+
columnOrder,
61
+
//...
62
+
}
63
+
onColumnOrderChange: setColumnOrder,
64
+
//...
65
+
});
66
+
```
67
+
68
+
### Reordering Columns
69
+
70
+
If the table has UI that allows the user to reorder columns, you can set up the logic something like this:
0 commit comments