Row selection
rowSelection enables the row selection column and manages single selection, multiple selection, select-all, custom batch selection and tree linkage in one place.
If your page needs things like batch actions, editing in a side panel after checking rows, or exporting the selected items, this is the set of features you will reach for most often.
Live demo
Controlled multiple selection, a single row disabled through getCheckboxProps, and the header selections dropdown (with one custom item).
Basic multiple selection
<script setup lang="ts">
import { VTable, type TableColumnsType, type RowSelection } from '@vtable-guild/vtable-guild'
interface UserRow {
key: string
name: string
disabled?: boolean
}
const columns: TableColumnsType<UserRow> = [{ title: 'Name', dataIndex: 'name', key: 'name' }]
const rowSelection: RowSelection<UserRow> = {
type: 'checkbox',
getCheckboxProps: (record) => ({
disabled: record.disabled,
}),
}
const dataSource: UserRow[] = [
{ key: '1', name: 'Ada' },
{ key: '2', name: 'Grace', disabled: true },
]
</script>
<template>
<VTable
row-key="key"
:columns="columns"
:data-source="dataSource"
:row-selection="rowSelection"
/>
</template>Controlled selection
When the selection has to stay in sync with page state, a side action panel or API requests, use controlled mode directly.
Passing selectedRowKeys enables controlled mode: application code owns the selected keys. The component requests an update through onChange and accepts the new selection only after the parent writes it back. Replacing dataSource does not automatically prune controlled keys, even when preserveSelectedRowKeys is omitted or explicitly set to false.
The snippet below reuses UserRow from the basic example. RowSelection<UserRow> gives inline callbacks their contextual types: the onChange parameters are Key[] and UserRow[], so you don't need to annotate them one by one.
import { computed, ref } from 'vue'
import type { Key, RowSelection } from '@vtable-guild/vtable-guild'
const selectedRowKeys = ref<Key[]>([])
const rowSelection = computed<RowSelection<UserRow>>(() => ({
type: 'checkbox',
selectedRowKeys: selectedRowKeys.value,
onChange: (keys) => {
selectedRowKeys.value = [...keys]
},
}))Single selection and batch selection
- type: 'radio' switches to single selection mode.
- selections: true enables the default batch selection menu.
- selections also accepts custom menu items, for application actions such as "select only the current results" or "select only actionable rows".
- hideSelectAll hides the select-all control in the header.
Pagination and cross-page selection
With pagination enabled, the header checkbox selects or deselects only the selectable rows on the current page; invert selection and Shift range selection are limited to the current page as well. With local pagination, switching pages keeps the selection on other pages.
selections: true enables the "Select all data / Invert current page / Clear all" menu, and [SELECTION_ALL] enables "Select all data" on its own. "Select all data" selects every loaded selectable record after filtering, regardless of the current page. Remote pagination usually loads only the current page, so this neither requests nor selects pages that have not been loaded.
Remote paging usually replaces dataSource. Whether keys survive across pages and whether callbacks can return records from earlier pages are separate questions:
| Selection mode | preserveSelectedRowKeys | Keys after replacing dataSource | rows in onChange |
|---|---|---|---|
Controlled: selectedRowKeys is passed | Omitted or false | Owned by application code; never automatically pruned | Selected records in the current dataSource only |
Uncontrolled: selectedRowKeys is omitted | Omitted or false | Keys absent from the current dataSource are automatically pruned | Selected records in the current dataSource only |
Controlled: selectedRowKeys is passed | true | Owned by application code; never automatically pruned | Current records plus previously cached selected records from other pages |
Uncontrolled: selectedRowKeys is omitted | true | Keys absent from the current dataSource are retained | Current records plus previously cached selected records from other pages |
For example, in controlled mode, select key 1 on page 1, then select key 11 on page 2. Without preserve enabled, onChange receives keys [1, 11], but rows contains only record 11 from page 2. Returning to page 1 still shows record 1 checked. The count based on keys can therefore exceed the number of rows.
Set preserveSelectedRowKeys: true when onChange: (keys: Key[], rows: TRecord[]) => void needs to return selected records from earlier pages. It caches loaded selected records while they remain selected. It never requests unloaded pages and cannot reconstruct records that have never been loaded or cached from keys alone. Give every record a stable rowKey.
If controlled selection must be cleared after changing pages, submitting a new query or deleting records, explicitly clear selectedRowKeys and any selected records stored by application code in that flow. Setting preserveSelectedRowKeys: false alone does not clear controlled keys.
Use the local pagination example to check the scope of the header select-all and the menu select-all, and the remote pagination example to check cross-page retention and the backfill of selected records.
Callback order
Selection callbacks return selectedRows in the order of the corresponding selectedRowKeys. With ordinary checkboxes, keys keep their insertion order and newly selected keys are appended. Controlled mode starts with the key order supplied by application code; tree linkage may append parent or child keys according to its linkage rules.
The component looks up each key in order, preferring records in the current data source and then checking the cross-page cache when preserveSelectedRowKeys is enabled. For example, selecting 3 and then 1 on the same page produces [3, 1] for both keys and row IDs. With preserve enabled, selecting 1 on page 1 and then 11 on page 2 produces [1, 11] for both; current-page records are not grouped ahead of cached records.
Keys whose records are unavailable are skipped, without placeholder rows. Rows therefore preserve the relative key order but can still be fewer than keys. The arrays correspond by index only when a record is available for every key. Use rowKey to associate records in application code.
For a single selection action, onChange and onSelect, onSelectAll or onSelectMultiple share the normalized selection result. Synchronous changes to keys or the data source in the parent's onChange do not cause subsequent callbacks to look up a different set of rows. changeRows contains records that actually changed in the action's traversal order, independently of the order of all selected rows.
Tree data linkage
When checkStrictly is false, parent and child nodes are linked:
- Once all child nodes are selected, the parent node is selected automatically.
- When only some child nodes are selected, the parent node shows as indeterminate.
- Clicking a parent node toggles its whole subtree recursively.
If your application cares more about selecting each row independently, keeping checkStrictly at true is clearer.
Usage tips
- Set
preserveSelectedRowKeys: trueto retain keys absent from the data source in uncontrolled mode, or to return selected records from earlier pages in callbacks. Application code owns the retention and clearing of controlled keys. - If some rows cannot be acted on, disable them through getCheckboxProps instead of rolling the state back after a click.
- Tree linkage suits permission trees, organization charts and grouped data, not tables where every row is semantically independent.
API
TRecord is the application row type. The tables below list only the options related to row selection; see the API Reference for all props.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
rowKey | string | ((record: TRecord) => Key) | 'key' | Unique row identity. Selection state is recorded by row key, so it must be stable and unique. |
rowSelection | RowSelection<TRecord> | - | Enables the row selection column; for its fields, see Row selection · RowSelection. |
getPopupContainer | (triggerNode: HTMLElement) => HTMLElement | - | Mount container for the filter, selection and page-size menus. |
RowSelection
| Field | Type | Default | Description |
|---|---|---|---|
type | 'checkbox' | 'radio' | 'checkbox' | Selection type. |
selectedRowKeys | Key[] | - | Controlled selected keys, owned by application code; data source changes never automatically prune them, regardless of preserve. |
defaultSelectedRowKeys | Key[] | - | Default selected keys. |
onChange | (selectedRowKeys: Key[], selectedRows: TRecord[]) => void | - | Selection change callback; rows follows key order, skipping unavailable records. Without preserve, records come only from the current data source; with preserve, they also include previously cached selected records from other pages. |
onSelect | (record: TRecord, selected: boolean, selectedRows: TRecord[]) => void | - | Single-row selection change callback; selectedRows matches the selection result of onChange for the same action. |
onSelectMultiple | (selected: boolean, selectedRows: TRecord[], changeRows: TRecord[]) => void | - | Shift multi-selection callback; selectedRows matches onChange for the same action, while changeRows keeps the action traversal order. |
onSelectAll | (selected: boolean, selectedRows: TRecord[], changeRows: TRecord[]) => void | - | Select-all change callback; selectedRows matches onChange for the same action, while changeRows keeps the action traversal order. |
onSelectInvert | (selectedRowKeys: Key[]) => void | - | Callback when the selection is inverted. |
onSelectNone | () => void | - | Callback when the selection is cleared. |
getCheckboxProps | (record: TRecord) => { disabled?: boolean; name?: string } | - | Injects attributes into the selection control. |
columnWidth | number | string | 48 | Width of the selection column. |
fixed | boolean | 'left' | 'right' | - | Fixed position of the selection column. |
columnTitle | string | VNodeChild | - | Header content of the selection column. |
renderCell | (value: boolean, record: TRecord, index: number, originNode: VNodeChild) => VNodeChild | RenderedCell | - | Custom selection cell. |
checkStrictly | boolean | true | Whether parent and child rows are selected independently in tree data. |
selections | boolean | (SelectionItem | SelectionSentinel)[] | false | Default or custom batch selection menu; for the default menu constants, see SelectionSentinel. |
hideSelectAll | boolean | false | Hides the select-all checkbox and the selection dropdown. |
preserveSelectedRowKeys | boolean | false | Retains uncontrolled selected keys when the data source changes and caches loaded selected records for cross-page callbacks; controlled keys remain owned by application code, and unloaded pages are never requested automatically. |
With pagination enabled, the header select-all and invert selection are limited to the current page; "Select all data" selects every loaded selectable record after filtering. See Pagination and cross-page selection.
Events
| Event | Payload | Description |
|---|---|---|
change | (filters: TableFiltersInfo, sorter: VTableSorterResult<TRecord>, extra: TableChangeExtra<TRecord>) => void | Single event outlet after sorting, filtering, selection and pagination; extra.action is 'sort', 'filter', 'select' or 'paginate'. |
When the selection changes, extra.action is 'select' and extra.currentDataSource is the data displayed on the current page. If you only care about the selected items, using rowSelection.onChange directly is simpler.
Theming
To change only colors, override the CSS variables first; to change classes, use the per-instance ui or the global theme.table.slots. See Three-layer theme overrides for how to choose between the three.
ui slots
| Slot | Description |
|---|---|
tdSelected | <td> background of selected rows. |
tdRowSelectedHover | <td> background overlay of selected rows on hover. |
selectionDropdown | Row selection dropdown panel. |
selectionDropdownItem | Row selection dropdown item. |
selectionExtra | Extra trigger icon in the header of the row selection column. |
Preset defaults
The default classes of both presets on the slots above, given in the shape of createVTableGuild({ theme }) so you can copy and modify them; overrides are merged with the defaults through cn(). element-plus matches the original Element Plus: selected rows do not change their background. In the default precompiled mode these utilities carry the vtg- prefix at runtime; see utility prefix and override rules for the override rules.
createVTableGuild({
theme: {
table: {
slots: {
tdSelected: 'bg-[color:var(--vtg-table-row-selected-bg)]',
tdRowSelectedHover: 'bg-[color:var(--vtg-table-row-selected-hover-bg)]',
selectionDropdown: '',
selectionDropdownItem: 'flex items-center cursor-pointer px-3 py-[5px] rounded-sm hover:bg-[color:var(--color-control-item-hover-bg)]',
selectionExtra: 'inline-flex items-center justify-center cursor-pointer ml-0.5 text-[10px] text-[color:var(--color-sorter-icon)] hover:text-[color:var(--color-primary)] transition-colors',
},
variants: {
striped: {
true: {
tdSelected: 'group-even/row:bg-[color:var(--vtg-table-row-selected-bg)]',
tdRowSelectedHover: 'group-even/row:bg-[color:var(--vtg-table-row-selected-hover-bg)]',
},
},
},
defaultVariants: {
striped: false,
},
},
},
})createVTableGuild({
themePreset: 'element-plus',
theme: {
table: {
slots: {
tdSelected: '',
tdRowSelectedHover: '',
selectionDropdown: 'min-w-[120px]',
selectionDropdownItem: 'flex items-center cursor-pointer px-3 py-1.5 rounded-sm hover:bg-[color:var(--color-control-item-hover-bg)]',
selectionExtra: 'inline-flex items-center justify-center cursor-pointer ml-1 text-xs text-[color:var(--color-muted)] hover:text-[color:var(--color-primary)] transition-colors',
},
},
},
})CSS variables
The table lists the defaults in light mode; dark mode overrides them separately under .dark. The popup variables are shared by table popups such as the batch selection menu and the filter panel.
| Variable | Purpose | antdv | element-plus |
|---|---|---|---|
--vtg-table-row-selected-bg | Selected row background | #e6f4ff | not read |
--vtg-table-row-selected-hover-bg | Selected row hover background | #bae0ff | not read |
--vtg-table-popup-radius | Radius of table popups such as filtering and batch selection | 6px | 2px |
--vtg-table-popup-border | Table popup border | 0 | 1px solid var(--vtg-border-color-lighter, var(--color-default)) |
--vtg-table-popup-padding-block | Table popup vertical padding | 4px | 5px |
--vtg-table-popup-padding-inline | Table popup horizontal padding | 4px | 0px |
--vtg-table-popup-shadow | Table popup shadow | 0 6px 16px rgb(0 0 0 / 8%), 0 3px 6px -4px rgb(0 0 0 / 12%), 0 9px 28px 8px rgb(0 0 0 / 5%) | 0 0 12px rgb(0 0 0 / 12%) |
Type Declarations
Every row callback in RowSelection<TRecord> keeps the application row type. The selection sentinel is the type of the SELECTION_ALL, SELECTION_INVERT and SELECTION_NONE constants; see the Type reference for how to import them and for the render return types.
View type declarations
export type Key = string | numberexport type RowSelectionType = 'checkbox' | 'radio'export interface SelectionItem {
key: string
text: string | VNodeChild
onSelect?: (changeableRowKeys: Key[]) => void
}export type SelectionSentinel =
| typeof SELECTION_ALL
| typeof SELECTION_INVERT
| typeof SELECTION_NONEexport interface RowSelection<TRecord extends object = Record<string, any>> {
preserveSelectedRowKeys?: boolean
type?: RowSelectionType
selectedRowKeys?: Key[]
defaultSelectedRowKeys?: Key[]
onChange?: (selectedRowKeys: Key[], selectedRows: TRecord[]) => void
onSelect?: (record: TRecord, selected: boolean, selectedRows: TRecord[]) => void
onSelectMultiple?: (selected: boolean, selectedRows: TRecord[], changeRows: TRecord[]) => void
onSelectAll?: (selected: boolean, selectedRows: TRecord[], changeRows: TRecord[]) => void
onSelectInvert?: (selectedRowKeys: Key[]) => void
onSelectNone?: () => void
getCheckboxProps?: (record: TRecord) => { disabled?: boolean; name?: string }
columnWidth?: number | string
fixed?: boolean | 'left' | 'right'
columnTitle?: string | VNodeChild
renderCell?: (
value: boolean,
record: TRecord,
index: number,
originNode: VNodeChild,
) => VNodeChild | RenderedCell
checkStrictly?: boolean
selections?: boolean | (SelectionItem | SelectionSentinel)[]
hideSelectAll?: boolean
}export interface RenderedCell {
props?: CellAdditionalProps
children?: VNodeChild
}export interface CellAdditionalProps {
class?: string
className?: string
style?: CSSProperties
colSpan?: number
rowSpan?: number
colspan?: number
rowspan?: number
onClick?: (event: MouseEvent) => void
onMouseenter?: (event: MouseEvent) => void
onMouseleave?: (event: MouseEvent) => void
[key: string]: unknown
}