Skip to content

Commit eef7a99

Browse files
authoredFeb 16, 2024··
docs: column ordering guide (#5353)
1 parent d155dff commit eef7a99

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
 

‎docs/guide/column-ordering.md

+78
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,93 @@ title: Column Ordering
77
Want to skip to the implementation? Check out these examples:
88

99
- [column-ordering](../framework/react/examples/column-ordering)
10+
- [column-ordering-dnd](../framework/react/examples/column-dnd)
1011

1112
## API
1213

1314
[Column Ordering API](../api/features/column-ordering)
1415

1516
## Column Ordering Guide
1617

18+
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+
1722
There are 3 table features that can reorder columns, which happen in the following order:
1823

1924
1. [Column Pinning](../guide/column-pinning) - If pinning, columns are split into left, center (unpinned), and right pinned columns.
2025
2. Manual **Column Ordering** - A manually specified column order is applied.
2126
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.
37+
38+
```jsx
39+
const table = useReactTable({
40+
//...
41+
initialState: {
42+
columnOrder: ['columnId1', 'columnId2', 'columnId3'],
43+
}
44+
//...
45+
});
46+
```
47+
48+
> **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+
const table = 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:
71+
72+
```tsx
73+
const [columnOrder, setColumnOrder] = useState<string[]>(columns.map(c => c.id));
74+
75+
//depending on your dnd solution of choice, you may or may not need state like this
76+
const [movingColumnId, setMovingColumnId] = useState<string | null>(null);
77+
const [targetColumnId, setTargetColumnId] = useState<string | null>(null);
78+
79+
//util function to splice and reorder the columnOrder array
80+
const reorderColumn = <TData extends RowData>(
81+
movingColumnId: Column<TData>,
82+
targetColumnId: Column<TData>,
83+
): string[] => {
84+
const newColumnOrder = [...columnOrder];
85+
newColumnOrder.splice(
86+
newColumnOrder.indexOf(targetColumnId),
87+
0,
88+
newColumnOrder.splice(newColumnOrder.indexOf(movingColumnId), 1)[0],
89+
);
90+
setColumnOrder(newColumnOrder);
91+
};
92+
93+
const handleDragEnd = (e: DragEvent) => {
94+
if(!movingColumnId || !targetColumnId) return;
95+
setColumnOrder(reorderColumn(movingColumnId, targetColumnId));
96+
};
97+
98+
//use your dnd solution of choice
99+
```

0 commit comments

Comments
 (0)
Please sign in to comment.