Skip to content
Niko Table

Dynamic Columns

Let users add, rename, move, delete, reorder and retype columns at runtime.

Use this when users should change the columns themselves, like in a spreadsheet.

  • useGridColumns owns the column list.
  • <DataGridColumns> connects the header menu and the add button.
  • New columns show empty cells. You change nothing in the grid engine.
grid-dynamic-columns.tsx
Feature
Owner
Status
Preview with Controlled State
grid-dynamic-columns-state.tsx
Feature
Owner
Status
Current Grid State
Live view of the grid engine state for demonstration
Focused Cell:None
Editing Cell:None
Selection Anchor:None
Total Rows:3
Column Count:3
Column Ids:feature, owner, status
Can Undo / Redo:No / No
Last Commit:None
pnpm dlx shadcn@latest add @niko-table/data-table-grid @niko-table/data-table-view-dnd-menu
const cols = useGridColumns({
initialColumns: [
{ id: "task", label: "Task", type: "text" },
{ id: "status", label: "Status", type: "select", options: ["Todo", "Done"] },
],
})
const grid = useDataGrid({ columnIds: cols.columnIds, createEmptyRow })

Header menu:

<DataTableColumnActions>
<DataTableColumnHideOptions />
<GridColumnMenuOptions />
</DataTableColumnActions>

Provider + toolbar:

<DataGrid grid={grid}>
<DataGridColumns value={cols}>
<DataGridToolbar>
<DataGridAddColumnButton />
<DataTableViewDndMenu
columnOrder={cols.columnIds}
onColumnOrderChange={cols.reorderColumns}
/>
</DataGridToolbar>
{/* table… */}
</DataGridColumns>
</DataGrid>

The hook returns: columns, columnIds, addColumn, renameColumn, moveColumn, removeColumn, reorderColumns, setColumnType.

<DataGridColumns
value={cols}
columnTypes={[
{ value: "text", label: "Text" },
{ value: "select", label: "Select", options: STATUSES },
]}
resolveCell={resolveCell}
>

Existing cells are checked again against the new type. Values that do not match turn invalid.