Skip to content

批量操作提示条 ​

VTableSelectionAlert 与表格共用受控的 selectedRowKeys。它按 keys 展示数量,提供清空入口和业务操作插槽,可放在表格上方、工具栏扩展行或分页内容插槽中。

跨页批量操作 ​

下面的示例用两个数据页模拟远程分页。分别在两页勾选记录,可以看到已选数量包含当前没有加载的记录,而插槽中的 selectedRows 只包含业务传入的已加载记录。批量操作使用 keys,不依赖当前页数据是否完整。

在 Playground 中编辑

提示条按受控 selectedRowKeys 统计数量,表格未开启 preserve 时也能显示业务保留的跨页 key 数量。如果需要表格选择回调返回此前页的选中记录,再给表格设置 preserveSelectedRowKeys: true;提示条插槽中的 selectedRows 由提示条的同名 prop 提供,不会自动读取表格的记录缓存。业务可以把表格回调返回的选中行传给提示条;未传 selectedRows 时,插槽得到空数组。详见分页与跨页选择。

提示条不会主动请求其他页,也不会把“已加载记录全选”转换成“服务端全部结果”。如果业务需要操作某个查询的全部结果,应另行定义服务端选择协议。

受控状态与清空 ​

vue
<VTableSelectionAlert
  v-model:selected-row-keys="selectedRowKeys"
  :selected-rows="availableSelectedRows"
  :loading="batchLoading"
  @clear="handleClear"
>
  <template #actions="{ selectedRowKeys, disabled }">
    <a-button :disabled="disabled" @click="approve(selectedRowKeys)">批量审核</a-button>
  </template>
</VTableSelectionAlert>

清空依次发出 update:selectedRowKeys([]) 和 clear(previousKeys, availableRows)。父组件接受更新前,显示数量保持传入值。组件不维护另一份选择状态,也不触发表格的 change 或刷新请求。

disabled 或 loading 会禁用默认清空入口,插槽中的 disabled 同时反映这两个状态。业务按钮自行绑定该值。默认没有选中项时隐藏整个提示条;alwaysShow 可以显示零选中状态,此时清空按钮禁用。

显隐过渡 ​

提示条默认使用约 180ms 的高度展开、收起与淡入淡出。高度按内容实际尺寸计算,支持文字和业务按钮换行;收起结束后移除占位。过渡只在提示条从隐藏变为显示或从显示变为隐藏时触发,选中数量变化只更新内容。

如果提示条与前面的工具栏按钮之间需要间距,将间距放在组件的折叠区域内,让它随提示条一起收起:

vue
<VTableSelectionAlert
  v-model:selected-row-keys="selectedRowKeys"
  style="--vtg-selection-alert-spacing-before: 16px"
/>

通过 --vtg-selection-alert-motion-duration 调整时长,设为 0s 可以关闭过渡。组件也会遵循系统的减少动态效果设置。alwaysShow 保持提示条可见,因此不会因选中数量归零而收起。

API ​

TRecord 表示业务行类型。

Props ​

Prop类型默认值说明
selectedRowKeysreadonly Key[]必填受控的已选键,数量只根据它计算。配合 v-model:selected-row-keys 使用。
selectedRowsreadonly TRecord[][]业务当前拥有的已选记录,可以少于 keys。
alwaysShowbooleanfalse零选中时仍显示提示条。
showClearbooleantrue是否显示默认清空入口。
disabledbooleanfalse禁用内置操作,并向业务插槽传递禁用状态。
loadingbooleanfalse批量操作加载状态,同时禁用清空。
localeobject-实例文案覆盖,字段见主题与语言。
uiobject-实例 slot class 覆盖,可用的 slot 见 ui slots。
classstring-根元素 class。

Events ​

事件参数说明
update:selectedRowKeys(keys: Key[]) => void请求更新已选键;清空时参数为 []。
clear(selectedRowKeys: readonly Key[], selectedRows: readonly TRecord[]) => void点击清空后触发,参数是清空前的键和可用记录。

Slots ​

showClear 控制的默认清空入口与 actions 一起放在右侧操作区。

Slot参数类型说明
infoTableSelectionAlertSlotProps<TRecord>替换左侧数量文本。
actionsTableSelectionAlertSlotProps<TRecord>右侧业务操作,与默认清空入口并排。

Slot Props ​

info 和 actions 都接收 TableSelectionAlertSlotProps<TRecord>。

字段类型说明
selectedRowKeysreadonly Key[]已选键的快照。
selectedRowsreadonly TRecord[]可用记录的快照。
countnumberkeys 数量。
disabledbooleandisabled || loading。
loadingboolean当前加载状态。
clear() => void与默认清空入口相同的受控更新请求。

VTable 行选择配置 ​

Prop类型默认值说明
rowSelectionRowSelection<TRecord>-开启行选择列,字段见行选择 · RowSelection。

主题与语言 ​

全局使用 theme.selectionAlert.slots,实例使用 ui。

文案跟随全局语言,支持 localeOverrides.selectionAlert 和实例 locale。selected 文案里的 {count} 替换为已选数量,label 是区域名称,clear 是清空按钮文本。

以下 class 和 CSS 变量控制提示条的布局、文案和清空入口。

ui slots ​

主题配置Slot说明
theme.selectionAlertwrapper显隐过渡与高度折叠的外层容器。
theme.selectionAlertcontent包含提示条前间距的内容容器。
theme.selectionAlertroot批量操作提示条视觉容器。
theme.selectionAlertinfo选中数量或 #info 自定义内容的容器。
theme.selectionAlerttext默认选中数量文字。
theme.selectionAlertclear清空选择按钮。
theme.selectionAlertoption清空选择与批量操作区域的容器。
theme.selectionAlertactions批量操作内容容器。

预设默认值 ​

ts
createVTableGuild({
  theme: {
    selectionAlert: {
      slots: {
        wrapper: '',
        content: '',
        root: 'box-border flex flex-wrap items-center justify-between gap-x-4 gap-y-2 rounded-[var(--vtg-selection-alert-radius,6px)] bg-[color:var(--vtg-selection-alert-bg,#fafafa)] px-[var(--vtg-selection-alert-padding-inline,24px)] py-[var(--vtg-selection-alert-padding-block,8px)] text-[length:var(--vtg-table-font-size)] font-[family-name:var(--font-family-base)] leading-[var(--vtg-table-line-height)] text-[color:var(--color-muted)]',
        info: 'flex min-w-0 flex-1 flex-wrap items-center',
        text: 'break-words',
        clear: 'shrink-0 border-0 bg-transparent p-0 text-[length:var(--vtg-table-font-size)] text-[color:var(--color-primary)] cursor-pointer hover:text-[color:var(--color-primary-hover)] disabled:cursor-not-allowed disabled:text-[color:var(--color-text-disabled)]',
        option: 'flex min-w-[48px] items-center gap-2 pl-4',
        actions: 'flex min-w-0 flex-wrap items-center gap-2',
      },
    },
  },
})
ts
createVTableGuild({
  themePreset: 'element-plus',
  theme: {
    selectionAlert: {
      slots: {
        wrapper: '',
        content: '',
        root: 'box-border flex flex-wrap items-center justify-between gap-x-4 gap-y-2 rounded-[var(--vtg-selection-alert-radius,6px)] bg-[color:var(--vtg-selection-alert-bg,#fafafa)] px-[var(--vtg-selection-alert-padding-inline,24px)] py-[var(--vtg-selection-alert-padding-block,8px)] text-[length:var(--vtg-table-font-size)] font-[family-name:var(--font-family-base)] leading-[var(--vtg-table-line-height)] text-[color:var(--color-muted)] rounded-[var(--vtg-selection-alert-radius,4px)]',
        info: 'flex min-w-0 flex-1 flex-wrap items-center',
        text: 'break-words',
        clear: 'shrink-0 border-0 bg-transparent p-0 text-[length:var(--vtg-table-font-size)] text-[color:var(--color-primary)] cursor-pointer hover:text-[color:var(--color-primary-hover)] disabled:cursor-not-allowed disabled:text-[color:var(--color-text-disabled)]',
        option: 'flex min-w-[48px] items-center gap-2 pl-4',
        actions: 'flex min-w-0 flex-wrap items-center gap-2 [&>.el-button+.el-button]:!ml-0',
      },
    },
  },
})

CSS 变量 ​

变量作用antdvelement-plus
--color-primary主题主色,用于拖拽手柄悬停、按钮高亮和字段状态等#1677ff#409eff
--vtg-selection-alert-radius批量操作提示条圆角6px4px
--vtg-selection-alert-bg批量操作提示条背景rgb(0 0 0 / 2%)#fafafa
--vtg-selection-alert-padding-inline批量操作提示条横向内边距24px24px
--vtg-selection-alert-padding-block批量操作提示条纵向内边距8px8px
--vtg-selection-alert-motion-duration提示条高度与透明度的过渡时长,设为 0s 可关闭过渡未声明,回退 180ms未声明,回退 180ms
--vtg-selection-alert-motion-easing提示条显隐过渡的缓动曲线未声明,回退 cubic-bezier(0.645, 0.045, 0.355, 1)未声明,回退 cubic-bezier(0.645, 0.045, 0.355, 1)
--vtg-selection-alert-spacing-before提示条前的间距,包含在展开、收起的高度中未声明,回退 0未声明,回退 0

Type Declarations ​

批量提示条类型
ts
import type { VNodeChild } from 'vue'
import type {
  Key,
  SlotProps,
  SelectionAlertThemeConfig,
  VTableGuildSelectionAlertLocale,
} from '@vtable-guild/vtable-guild'

export interface TableSelectionAlertProps<TRecord extends object = Record<string, unknown>> {
  selectedRowKeys: readonly Key[]
  selectedRows?: readonly TRecord[]
  alwaysShow?: boolean
  disabled?: boolean
  loading?: boolean
  showClear?: boolean
  locale?: Partial<VTableGuildSelectionAlertLocale>
  ui?: SlotProps<SelectionAlertThemeConfig>
  class?: string
}

export interface TableSelectionAlertSlotProps<TRecord extends object = Record<string, unknown>> {
  selectedRowKeys: readonly Key[]
  selectedRows: readonly TRecord[]
  count: number
  disabled: boolean
  loading: boolean
  clear: () => void
}

export interface TableSelectionAlertSlots<TRecord extends object = Record<string, unknown>> {
  info?: (props: TableSelectionAlertSlotProps<TRecord>) => VNodeChild
  actions?: (props: TableSelectionAlertSlotProps<TRecord>) => VNodeChild
}
ts
export interface VTableGuildSelectionAlertLocale {
  label: string
  selected: string
  clear: string
}
ts
export type SelectionAlertThemeConfig = typeof selectionAlertTheme
export type SelectionAlertSlots = keyof SelectionAlertThemeConfig['slots']
export type QueryThemeConfig = typeof queryTheme
export type QuerySlots = keyof QueryThemeConfig['slots']