DocsGuides

Guides

Row Selection Guide

How to add and customize row selection checkboxes in Shadcn Table

Shadcn Table has a built-in row selection feature and makes it easy to manage the selection state yourself. This guide demonstrates how to enable row selection and customize the selection behavior.

Example

You can combine the row pinning feature with the row selection feature to create a cool user experience where selected rows always stay within view.

Storybook

Relevant Table Options

Prop NameTypeDescription
enableMultiRowSelectionbooleanIf true, the user can select multiple rows at once with a checkbox. If false, the user can only select one row at a time with a radio button.
enableRowRangeSelectionbooleanIf true, the user can select a range of rows at once by holding down the shift key while clicking a row or its checkbox. Customise what counts as a range-selection event with the isRowRangeSelectionEvent option.
enableRowSelectionboolean | (row: ST_Row) => booleanEnables row selection either for every row, or on a per-row basis using a function.
enableSelectAllboolean
enableSubRowSelectionboolean
getRowId(originalRow: TData, index: number, parent?: ST_Row<TData>) => stringThis optional function is used to derive a unique ID for any given row. If not provided the rows index is used (nested rows join together with . using their grandparents' index eg. index.index.index). If you need to identify individual rows that are originating from any server-side operations, it's suggested you use this function to return an ID that makes sense regardless of network IO/ambiguity eg. a userId, taskId, database ID field, etc.
isRowRangeSelectionEvent(event: unknown) => booleanDecides whether a row selection event should select a range of rows (by default, events with shiftKey on the event or on event.nativeEvent).
onRowSelectionChangeOnChangeFn<RowSelectionState>If provided, this function will be called with an updaterFn when state.rowSelection 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.
positionToolbarAlertBanner'bottom' | 'top' | 'none'
renderToolbarAlertBannerContent({ table, groupedAlert, selectedAlert }) => ReactNode
rowPinningDisplayMode'sticky' | 'top' | 'bottom' | 'top-and-bottom' | 'select-sticky' | 'select-top' | 'select-bottom'
selectAllMode'all' | 'page'
stSelectAllCheckboxPropsCheckboxProps | ({ table }) => CheckboxProps
stSelectCheckboxPropsCheckboxProps | ({ row, table }) => CheckboxProps

Relevant State

State OptionTypeDescription
rowSelectionRecord<string, boolean>

Enable Row Selection

Selection checkboxes can be enabled with the enableRowSelection table option.

const table = useShadcnTable({
  columns,
  data,
  enableRowSelection: true,
});

return <ShadcnTable table={table} />;

Enable Row Selection Conditionally Per Row

You can also enable row selection conditionally per row with the same enableRowSelection table option, but with a callback function instead of a boolean.

const table = useShadcnTable({
  columns,
  data,
  enableRowSelection: (row) => row.original.age > 18, //disable row selection for rows with age <= 18
});

Batch Row Selection

By default, when the user holds down the Shift key and clicks a row, all rows between the last selected row and the clicked row will be selected. This is TanStack Table's range selection; it can be disabled with the enableRowRangeSelection table option, and the isRowRangeSelectionEvent option decides which events count as a range selection.

const table = useShadcnTable({
  columns,
  data,
  enableRowSelection: true,
  enableRowRangeSelection: false, //disable batch row selection with shift key
});

Access Row Selection State

There are two ways to access the selection state. You can either manage the selection state yourself or read it from the table instance.

Manage Row Selection State

The row selection state is managed internally by default, but you will more than likely want to have access to that state yourself. Here is how you can simply get access to the row selection state, specifically:

const [rowSelection, setRowSelection] = useState<ST_RowSelectionState>({}); //ts type available

const table = useShadcnTable({
  columns,
  data,
  enableRowSelection: true,
  onRowSelectionChange: setRowSelection,
  state: { rowSelection },
});

return <ShadcnTable table={table} />;

Read Row Selection State from Table Instance

Alternatively, you can read the selection state directly from the table instance like so:

const table = useShadcnTable({
  columns,
  data,
  enableRowSelection: true,
  renderTopToolbarCustomActions: ({ table }) => (
    <Button
      onClick={() => {
        const rowSelection = table.state.rowSelection; //read state
        const selectedRows = table.getSelectedRowModel().rows; //or read entire rows
      }}
    >
      Download Selected Users
    </Button>
  ),
});

useEffect(() => {
  //fetch data based on row selection state or something
}, [table.state.rowSelection]);

return <ShadcnTable table={table} />;

Note: table.getSelectedRowModel().rows only really works with client-side pagination. Use rowSelection state if you are using server-side pagination. Row Models only contain rows based on the data you pass in.

Useful Row IDs

By default, the row.id for each row in the table is simply the index of the row in the table. You can override this and instruct Shadcn Table to use a more useful Row ID with the getRowId table option. For example, you may want something like this:

const table = useShadcnTable({
  columns,
  data,
  enableRowSelection: true,
  getRowId: (originalRow) => originalRow.userId,
});

As rows get selected, the rowSelection state will look like this:

{
  "3f25309c-8fa1-470f-811e-cdb082ab9017": true,
  "be731030-df83-419c-b3d6-9ef04e7f4a9f": true,
  ...
}

This can be very useful when you are trying to read your selection state and do something with your data as the row selection changes.

Storybook

Select Row on Row Click

By default, a row can only be selected by either clicking the checkbox or radio button in the st-row-select column. If you want to be able to select a row by clicking anywhere on the row, you can add your own onClick function to a table body row like this:

const table = useShadcnTable({
  columns,
  data,
  enableRowSelection: true,
  //clicking anywhere on the row will select it
  stTableBodyRowProps: ({ row, staticRowIndex, table }) => ({
    onClick: (event) =>
      getST_RowSelectionHandler({ row, staticRowIndex, table })(event), //import this helper function from shadcn-table
    style: { cursor: 'pointer' },
  }),
});

Disable Select All

By default, if you enable selection for each row, there will also be a select all checkbox in the header of the checkbox column. It can be hidden with the enableSelectAll table option.

const table = useShadcnTable({
  columns,
  data,
  enableRowSelection: true,
  enableSelectAll: false,
});

Position or Hide Alert Banner Selection Message

By default, an alert banner will be show as rows are selected. You can use the positionToolbarAlertBanner table option to show the alert banner in the top toolbar, bottom toolbar, as an overlay in the table head, or hide it completely.

const table = useShadcnTable({
  columns,
  data,
  enableRowSelection: true,
  positionToolbarAlertBanner: 'head-overlay', //show alert banner over table head, top toolbar, or bottom toolbar
});

Single Row Selection

By default, the enableMultiRowSelection table option is set to true, which means that multiple rows can be selected at once with a checkbox. If you want to only allow a single row to be selected at a time, you can set this table option to false and a radio button will be used instead of a checkbox.

const table = useShadcnTable({
  columns,
  data,
  enableMultiRowSelection: false, //shows radio buttons instead of checkboxes
  enableRowSelection: true,
});
Storybook

The radios form one group: arrow keys move the selection between rows.

Customize Select Checkboxes or Radio Buttons

The selection checkboxes can be customized with the stSelectCheckboxProps table option. Any prop that can be passed to a shadcn/ui Checkbox component can be specified here. For example, you may want to use a different color for the checkbox or use some logic to disable certain rows from being selected.

const table = useShadcnTable({
  columns,
  data,
  enableRowSelection: true,
  stSelectCheckboxProps: {
    color: 'secondary',
  },
});
Storybook

Manual Row Selection Without Checkboxes

You may have a use case where you want to be able to select rows by clicking them, but you do not want to show any checkboxes or radio buttons. You can do this by implementing a row selection feature yourself while keeping the enableRowSelection table option false so that the default selection behavior is disabled.

Storybook

View Extra Storybook Examples

On this page