DocsGuides

Guides

Column Ordering/DnD Guide

How to use and customize the column ordering and drag and drop features of Shadcn Table

Whether you just want to change the default column order in your table or let columns be reordered by dragging and dropping, Shadcn Table has a simple API for this. Column ordering is on by default: users drag a header cell to move its column.

Example

Storybook

Relevant Table Options

Prop NameTypeDescription
enableColumnDraggingboolean
enableColumnOrderingbooleanLets the user reorder columns by dragging a header cell. Set it to false to turn reordering off for the whole table, or set enableColumnOrdering: false on one column to lock it in place.
onColumnOrderChangeOnChangeFn<ColumnOrderState>If provided, this function will be called with an updaterFn when state.columnOrder changes. This overrides the default internal state management, so you will need to persist the state change either fully or partially outside of the table.
onDraggingColumnChangeOnChangeFn<ST_Column<TData> | null>Receives a function updater, as the TanStack Table on*Change handlers do. Resolve it with functionalUpdate, or pass a React state setter.
onHoveredColumnChangeOnChangeFn<ST_Column<TData> | null>Receives a function updater, as the TanStack Table on*Change handlers do. Resolve it with functionalUpdate, or pass a React state setter.
showColumnDragHandlebooleanRenders a grip button in each draggable header cell. The header cell itself is the drag source either way.

Relevant Column Options

Column OptionTypeDescription
enableColumnDraggingbooleanEnable or disable column dragging for this column.
enableColumnOrderingbooleanSet it to false to lock the column: it cannot be dragged, and while another column drags it shows a lock with "Fixed" and refuses the drop. The column menu shows a disabled "Fixed position" item. The built-in display columns other than row actions are locked by default.

Relevant State

State OptionTypeDescription
columnOrderArray<string>an array of strings representing the current order of the table columns
draggingColumnST_Column | nulla variable representing the currently dragged column
hoveredColumnST_Column | null

Change the Default Column Order

By Default, Shadcn Table will order the columns in the order they are defined in the columns table option. And Display Columns such as Actions, Selection, Expansion, etc., get added to either the beginning or the end of the table. You can customize all of this by defining your own columnOrder state and passing it either to the initialState or state table options.

The columnOrder state is an array of string column ids, that come from the ids or accessorKeys that you defined in your column definitions.

If you are enabling features that generate built-in ST display columns such as enableRowSelection, enableRowNumbers, enableRowActions, etc., you should include those column ids in your columnOrder state at the index you want them to appear in the table. If you do not include them, ST should automatically insert them at either the beginning or the end of the table just fine, but it might not be in the order you want.

See the Display Columns Guide for more information on what "st-row-\*" column ids are available.

const table = useShadcnTable({
  data,
  columns,
  enableRowSelection: true,
  initialState: {
    columnOrder: [
      'name',
      'email',
      'phone',
      'city',
      'country',
      'st-row-select', //move the built-in selection column to the end of the table
    ],
  },
});

return <ShadcnTable table={table} />;

Note: If the number of columns and the length of the columnOrder state do not match, ST will automatically re-generate the columnOrder state internally.

Manage Column Order State

If you need easier access to the columnOrder state, you can store the column order in your own state management, and then pass it back into the ST state table option and sync back up internal mutations with the onColumnOrderChange callback.

You should also initialize the columnOrder state yourself for the best results. Though, if you do not initialize the columnOrder state, as Shadcn Table will generate a default column order for you based on the order of the columns passed in the columns option, so you only need to properly initialize the column order state if there is a problem with the default order.

const columns = [
  //...
];

//easy shortcut to initialize the columnOrder state as array of column ids
const [columnOrder, setColumnOrder] = useState(
  ['st-row-select', ...columns.map((c) => c.accessorKey)], //array of column ids (Initializing is optional)
);

const table = useShadcnTable({
  data,
  columns,
  enableRowSelection: true,
  state: {
    columnOrder,
  },
  onColumnOrderChange: setColumnOrder,
});

return <ShadcnTable table={table} />;

Column Ordering with Drag and Drop

enableColumnOrdering is true by default. The header cell itself is the drag source: the cursor turns into a grab hand over a header that can be dragged, a click still opens the column menu, and only a real drag moves the column. Any cell of another column is a drop target, and a column dropped on a pinned column takes that column's pinning. Display columns such as row numbers and selection are not draggable; the row actions column takes drops and moves through the columns menu, but its header is not a drag source.

Pressing Escape or dropping outside the table cancels the drag and leaves the order as it was. While a drag is near the start or end edge of the scrolling (unpinned) area, the table scrolls sideways towards that edge, faster the closer the pointer is to it.

Set enableColumnOrdering: false to turn reordering off for the whole table.

const table = useShadcnTable({
  data,
  columns,
  enableColumnOrdering: false, //columns keep their order, headers are not draggable
});

enableColumnDragging alone makes headers draggable onto the grouping drop zone without reordering; the drop targets between columns come from enableColumnOrdering.

Storybook

Show a Drag Handle

The header needs no grip, but if you want a visible handle in each draggable header cell, set showColumnDragHandle: true. The header cell stays the drag source either way. Use stColumnDragHandleProps to customize the handle.

const table = useShadcnTable({
  data,
  columns,
  showColumnDragHandle: true, //render a grip button in each draggable header cell
});

Locked Columns

A column with enableColumnOrdering: false in its column definition is locked. It looks like any other column at rest, shows no grab cursor and cannot be dragged. While another column is being dragged, every cell of the locked column shows diagonal stripes, its header shows a lock icon with "Fixed" (the icon alone on a narrow column), and the pointer shows that a drop is not allowed there. Its column menu lists a disabled "Fixed position" item with the lock icon.

Which data columns are locked is up to you. The built-in display columns (row numbers, selection, expand and the like) are locked by default; the row actions column is not. With enableColumnOrdering: false on the table, every column menu shows the disabled "Fixed position" item.

const columns = [
  {
    accessorKey: 'id',
    header: 'ID',
    enableColumnOrdering: false, //this column keeps its place
  },
  //...
];

The "Fixed" and "Fixed position" texts come from the fixed and fixedPosition localization keys, and the lock from the LockIcon icon key.

Drag Animation

While a column is dragged, only the header row previews the move: the other headers slide aside to open a gap where the column will land, and the dragged header sits in that gap, faded with a dashed outline. The body does not move during the drag, and the column order changes only on drop. On drop the body columns slide to catch up with the header and the moved column flashes a tint for 600 ms. Dragging back over the column's own place restores the preview, and dropping there changes nothing. A cancelled drag slides the headers back. A move made in the columns menu slides the header and the body together and flashes the same way.

Only a reorder the user makes animates. Hydration, a stored view and Reset view apply the order at once. The slides are CSS transitions and compositor animations; nothing writes styles on every frame.

  • With reduced motion turned on in the operating system, nothing slides. The highlight stays, and the dragged column and the drop target show dashed borders instead.
  • Tables with header groups and tables with column virtualization get the highlight only.
  • With row virtualization the header previews the move and the body does not slide.
  • The body slide is skipped above 200 rendered rows.
  • A drop into a pinned area is not previewed.

View Extra Storybook Examples

On this page