Skip to content
Niko Table

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.

column-dnd.tsx
Employee IDNameDepartmentRoleLocationStatus
EMP-001
Alice Johnson
EngineeringSenior DeveloperNew Yorkactive
EMP-002
Bob Smith
DesignUI DesignerSan Franciscoremote
EMP-003
Carol Williams
MarketingMarketing LeadChicagoactive
EMP-004
David Brown
EngineeringBackend DeveloperAustinon-leave
EMP-005
Eva Martinez
ProductProduct ManagerSeattleactive
EMP-006
Frank Lee
EngineeringDevOps EngineerDenverremote
EMP-007
Grace Kim
DesignUX ResearcherPortlandactive
EMP-008
Henry Davis
SalesAccount ExecutiveBostonactive
Preview with Controlled State
column-dnd-state.tsx
Employee IDNameDepartmentRoleLocationStatus
EMP-001
Alice Johnson
EngineeringSenior DeveloperNew Yorkactive
EMP-002
Bob Smith
DesignUI DesignerSan Franciscoremote
EMP-003
Carol Williams
MarketingMarketing LeadChicagoactive
EMP-004
David Brown
EngineeringBackend DeveloperAustinon-leave
EMP-005
Eva Martinez
ProductProduct ManagerSeattleactive
EMP-006
Frank Lee
EngineeringDevOps EngineerDenverremote
EMP-007
Grace Kim
DesignUX ResearcherPortlandactive
EMP-008
Henry Davis
SalesAccount ExecutiveBostonactive
Column DnD State
Header drag, view-menu drag, visibility toggles, and search — all live state.
Total Columns:6
Current Order:id → name → department → role → location → status
Hidden:none
Search:—
View Full State
{
  "columnOrder": [
    "id",
    "name",
    "department",
    "role",
    "location",
    "status"
  ],
  "columnVisibility": {},
  "globalFilter": ""
}

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.
virtualized-column-dnd.tsx
Employee IDNameEmailDepartmentRoleLocationStatusSalary

Column DnD lets users reorder columns by dragging headers. It uses @dnd-kit and small composable pieces, in the shadcn/ui open-code style.

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-dnd

First 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.

Three pieces work together:

  1. DataTableColumnDndProvider: wraps the table in a horizontal DnD context
  2. DataTableDndHeader: renders headers as draggable items
  3. DataTableDndColumnBody: cells follow the column being dragged
column-dnd.tsx
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>
)

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:

column-dnd.tsx
<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.

  • Give every column an id. The grid needs it to track column order.
  • Pass columnOrder to both DataTableRoot and DataTableColumnDndProvider. Also pass it to DataTableViewDndMenu if 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. DataTableViewDndMenu and the header drag stay in sync.

✅ 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