Row selection
A checkbox on each row, select all for the page, and actions for the selected rows.
The selection column
createSelectionColumn comes with the table UI. It adds a checkbox to each row, and one in the header that selects the whole page:
import { createSelectionColumn } from "@/components/data-table/data-table-selection-column"
const columns = [createSelectionColumn<Order>(), ...otherColumns]- It's pinned to the start. It can't be hidden, sorted, resized, moved or unpinned, and it has no ⋯ menu, so nothing covers the select-all checkbox.
- It isn't listed in the Columns menu.
- Pass table messages to translate its labels:
createSelectionColumn<Order>(viTableMessages).
The pagination shows how many rows are selected ("2 of 20 selected").
Which rows can be selected
Choose with useDataTable({ enableRowSelection }): true, or a function of the row. Other rows show a disabled checkbox.
const table = useDataTable({
data,
columns,
getRowId: (order) => order.id,
enableRowSelection: (row) => row.original.status !== "paid",
})The selection only covers the current page, and clears when the page, sort, search or filter changes. getRowId keeps it across refetches of the same page.
Act on the selected rows
Pass selectionActions to the toolbar. While rows are selected, a bar floats over the bottom of the table with × to clear the selection, their count ("3 of 20 selected") and your actions:
<DataTableToolbar
table={table}
selectionActions={(rows) => (
<>
<Button variant="ghost" size="sm" onClick={() => exportOrders(rows)}>
Export
</Button>
<Button variant="ghost" size="sm" onClick={() => deleteOrders(rows)}>
Delete
</Button>
</>
)}
>
<DataTableSearch table={table} />
</DataTableToolbar>With editable columns, the bar also shows Bulk edit, after your actions. See Bulk edit. Without selectionActions or editable columns, there's no bar: the pagination already counts the selection, and the header checkbox clears it.
row.originalis the row's data,row.idits id fromgetRowId.- Actions only get the selected rows the page shows, never one the user can't see.
- When an action removes the rows (and the focused button with them), focus moves to the toolbar.
The floating bar
- It's centred on the table and stays in view: at the bottom of the screen while the table goes past it, and just above the table's bottom edge once that's in view, so it never covers the pagination.
- Its colors are the theme's swapped: dark on a light page, light on a dark one. Give your actions
variant="ghost": buttons in the bar hover in its colors. - It's a toolbar named by its count. ← → move between its buttons (mirrored right to left), Home and End go to the first and last, and Tab still reaches each one.
- It slides in and out, and simply appears and goes with reduced motion.
- It's
position: fixed. An ancestor with atransform,filterorperspectivemakes it fixed to that ancestor instead of the screen.
To put the bar somewhere else, render DataTableSelectionBar yourself.
From code
table.getSelectedRowModel().rows // the selected rows
row.getIsSelected()
row.toggleSelected(true)
table.resetRowSelection(true) // clears it