Column DnD Table
Let users drag column headers to reorder columns.
Users drag column headers to reorder columns. DataTableColumnDndProvider does this on top of @dnd-kit. Use it when people want their own column layout.
Unlike row DnD, column DnD works with sorting, filtering and virtualization. Column order does not depend on the rows.
Overview
Section titled “Overview”Preview with Controlled State
Virtualized Column DnD
Section titled “Virtualized Column DnD”For large datasets, use DataTableVirtualizedDndHeader and DataTableVirtualizedDndColumnBody. They combine row virtualization with column drag-and-drop.
- The
<DataTableColumnResize />marker works the same way. - Drag a header to reorder.
- Drag the edge grip to resize. The grip stops propagation, so it does not start a reorder.
Introduction
Section titled “Introduction”Column DnD lets users reorder columns by dragging headers. It uses @dnd-kit and small composable pieces, in the shadcn/ui open-code style.
Installation
Section titled “Installation”Install the DataTable core and the column DnD add-ons. data-table-view-dnd-menu and data-table-search-filter give you the example toolbar:
pnpm dlx shadcn@latest add @niko-table/data-table @niko-table/data-table-column-dnd @niko-table/data-table-column-resize @niko-table/data-table-view-dnd-menu @niko-table/data-table-search-filter @niko-table/data-table-virtualized @niko-table/data-table-virtualized-column-dndFirst time using
@niko-table? See the Installation Guide to set up the registry.
For other add-ons or manual copy-paste, see the Installation Guide.
Basic Column DnD
Section titled “Basic Column DnD”Three pieces work together:
DataTableColumnDndProvider: wraps the table in a horizontal DnD contextDataTableDndHeader: renders headers as draggable itemsDataTableDndColumnBody: cells follow the column being dragged
import { DataTableDndHeader, DataTableDndColumnBody,} from "@/components/niko-table/core/data-table-column-dnd-structure"
const [columnOrder, setColumnOrder] = React.useState<string[]>(() => columns.map(c => c.id as string),)
return ( <DataTableRoot data={data} columns={columns} state={{ columnOrder }} onColumnOrderChange={setColumnOrder} > <DataTableColumnResize /> <DataTableColumnDndProvider columnOrder={columnOrder} onColumnOrderChange={setColumnOrder} > <DataTable> <DataTableDndHeader /> <DataTableDndColumnBody /> </DataTable> </DataTableColumnDndProvider> </DataTableRoot>)Two Reorder Surfaces, One State
Section titled “Two Reorder Surfaces, One State”DataTableViewDndMenu is the View menu with drag-to-reorder. It shares the same columnOrder state as the header drag. Users get two ways to reorder. Visibility toggles and a Reset button sit in the same dropdown:
<DataTableToolbarSection className="justify-between"> <DataTableSearchFilter placeholder="Search employees..." /> <DataTableViewDndMenu columnOrder={columnOrder} onColumnOrderChange={setColumnOrder} onReset={() => setColumnOrder(initialColumnOrder)} /></DataTableToolbarSection>Do not need drag-to-reorder in the menu? Use DataTableViewMenu instead. It skips the @dnd-kit/* bundle.
Key Points
Section titled “Key Points”- Give every column an
id. The grid needs it to track column order. - Pass
columnOrderto bothDataTableRootandDataTableColumnDndProvider. Also pass it toDataTableViewDndMenuif you use it. - Headers are draggable by default. Grab any header to reorder.
- Safe with sorting and filtering. Column order does not depend on data order, unlike Row DnD.
- One shared state.
DataTableViewDndMenuand the header drag stay in sync.
When to Use
Section titled “When to Use”✅ Use Column DnD when:
- Users want to customize the table layout
- Different users want different column orders
- You are building configurable dashboards
❌ Consider other options when:
- Column order is fixed by design
- The table has very few columns
Next Steps
Section titled “Next Steps”- Row DnD Table: drag-and-drop row reordering
- Virtualization Table: virtual scrolling for large datasets
- Column Pinning Table: pin columns to the edges