行选择
rowSelection 用来开启行选择列,并统一管理单选、多选、全选、自定义批量选择和树形联动行为。
如果你的页面有“批量操作”“勾选后侧边编辑”“按选中项导出”这类需求,这一组能力会是高频入口。
在线示例
受控多选 + getCheckboxProps 禁用单行 + 表头「选择项」下拉(含一个自定义项)。
基础多选
<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: '姓名', 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>受控选择
当选中项需要和页面状态、侧边栏操作区或接口请求保持同步时,建议直接使用受控模式。
下面沿用基础示例中的 UserRow。RowSelection<UserRow> 为内联回调提供上下文类型:onChange 的参数分别是 Key[] 和 UserRow[],无需再逐个手工标注。
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]
},
}))单选与批量选择
- type: 'radio' 时进入单选模式。
- selections: true 可以开启默认批量选择菜单。
- selections 也可以传入自定义菜单项,用于“只选当前结果”“只选可操作项”等业务动作。
- hideSelectAll 可以隐藏表头全选入口。
分页与跨页选择
开启 pagination 后,表头复选框只选择或取消当前页的可选行;反选和 Shift 连选也限定当前页。本地分页切换页码时会保留其他页的选择。
selections: true 开启“全选所有 / 反选当页 / 清空所有”菜单,也可通过 [SELECTION_ALL] 单独开启“全选所有”。“全选所有”(英文 Select all data)选择筛选后全部已加载的可选记录,不受当前页限制。远程分页通常只加载当前页,不会因此请求或选择尚未加载的页。
远程翻页会替换 dataSource,需设置 preserveSelectedRowKeys: true 保留跨页 key 和已选记录,使 onChange: (keys: Key[], rows: TRecord[]) => void 能返回此前页的选中记录。请为每条记录提供稳定的 rowKey。
可在本地分页示例验证表头全选与菜单全选的范围,在远程分页示例验证跨页保留与已选记录回填。
树形数据联动
当 checkStrictly 为 false 时,父子节点会联动:
- 选中所有子节点后,父节点自动选中。
- 只选中部分子节点时,父节点显示为半选。
- 点击父节点会递归切换整棵子树。
如果你的业务更强调“每一行独立选择”,把 checkStrictly 保持为 true 会更清晰。
使用建议
- 需要长期保留选中结果时,使用 preserveSelectedRowKeys。
- 如果某些行不可操作,优先通过 getCheckboxProps 禁用,而不是在点击后再回滚状态。
- 树形联动适合权限树、组织架构和分组数据,不适合每行语义完全独立的表格。
相关页面
Type Declarations
RowSelection<TRecord> 中所有行回调都保留业务行类型。选择哨兵是 SELECTION_ALL、SELECTION_INVERT、SELECTION_NONE 常量的类型;导入方式与渲染返回类型见类型参考。
查看类型声明
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
}