自定义行与插槽
这部分能力用于把表格接进更复杂的业务界面。
当默认列渲染已经不够,但你又不想放弃排序、筛选、对齐和主题系统时,customRow、customHeaderRow、headerCell、bodyCell 和 summary 这些入口会更合适。
在线示例
headerCell 改表头文案、bodyCell 把状态换成徽标、customRow 给整行挂点击事件。
常见入口
<script setup lang="ts">
import { ref } from 'vue'
import { VTable, type TableColumnsType, type TableProps } from '@vtable-guild/vtable-guild'
interface ReportRow {
key: string
status: 'active' | 'paused'
score: number
}
const columns: TableColumnsType<ReportRow> = [
{ title: '状态', key: 'status', dataIndex: 'status' },
{ title: '分数', key: 'score', dataIndex: 'score' },
]
const dataSource: ReportRow[] = [{ key: '1', status: 'active', score: 95 }]
const clickedRowKey = ref('')
const rowClassName: NonNullable<TableProps<ReportRow>['rowClassName']> = (record) =>
record.status === 'paused' ? 'paused-row' : ''
const customRow: NonNullable<TableProps<ReportRow>['customRow']> = (record) => ({
onClick: () => {
clickedRowKey.value = record.key
},
})
const customHeaderRow: NonNullable<TableProps<ReportRow>['customHeaderRow']> = (
_columns,
index,
) => ({ 'data-header-row': index ?? 0 })
</script>
<template>
<VTable
row-key="key"
:columns="columns"
:data-source="dataSource"
:row-class-name="rowClassName"
:custom-row="customRow"
:custom-header-row="customHeaderRow"
>
<template #headerCell="{ title, column }">
<span v-if="column.key === 'score'">{{ title }} 指标</span>
<template v-else>{{ title }}</template>
</template>
<template #bodyCell="{ text, column }">
<span v-if="column.key === 'status'" class="badge">{{ text }}</span>
<template v-else>{{ text }}</template>
</template>
<template #summary>
<tr>
<td colspan="2">汇总信息</td>
</tr>
</template>
</VTable>
<p>最近点击:{{ clickedRowKey }}</p>
</template>单独声明回调时可使用 TableProps<ReportRow> 对应属性的类型;列回调则使用 ColumnType<ReportRow>。headerCell 和 bodyCell 由 TableSlotsDecl<ReportRow> 声明,record 会保持业务行类型。bodyCell.text 是 unknown,需要具体类型时先收窄,或者读取已知类型的 record 字段。
每个入口适合做什么
- rowClassName,根据行数据返回 class。
- customRow,给某一行注入属性、事件和 style。
- customHeaderRow,给表头行注入属性。
- customHeaderCell,给表头单元格补充属性。
- headerCell / bodyCell,替换单元格内部内容,同时保留表格内部交互能力。
- summary,在表体后追加摘要区域。
- paginationItem,在分页布局指定的位置显示选择数量、刷新按钮等业务内容。
分页行中的业务内容
用 pagination.layout 安排 { slot: '名称' },再通过 #paginationItem 的 name 区分内容。选择状态继续由父组件维护:
<VTable
:columns="columns"
:data-source="dataSource"
:row-selection="rowSelection"
:pagination="{
layout: {
left: [{ slot: 'selection' }],
right: ['prev', 'pager', 'next'],
},
}"
>
<template #paginationItem="{ name }">
<span v-if="name === 'selection'">已选 {{ selectedRowKeys.length }} 条</span>
</template>
</VTable>插槽同时接收 current、pageSize、total、pageCount、range、disabled、position 和 region,类型为 TablePaginationSlotProps。业务按钮自行绑定禁用状态并调用加载函数。分页隐藏时扩展内容也隐藏;需要在空数据时保留的操作放在 title、footer 或外部工具栏。完整示例见分页布局与内容插槽。
什么时候该用 ui,什么时候该用 slot
- 只是改样式,优先用 ui 和主题覆盖。
- 需要改内容结构或注入业务事件,再使用插槽和 customRow。
- 需要按记录动态附加属性时,优先用 customRow,而不是操作 DOM。
使用建议
- 结构级扩展尽量通过组件提供的入口完成,不要在 mounted 后再手动查 DOM 改写。
- bodyCell 更适合做状态标签、图标、补充文案等内容级增强。
- 单元格或整行编辑也通过 bodyCell 组合;草稿、校验和提交由业务管理,完整示例见编辑。
- 如果同一类样式会反复出现,优先沉淀到主题和 ui,而不是在每张表里重复写 slot 模板。
API
TRecord 表示业务行类型。表格的完整配置见 API Reference。
Props
| Prop | 类型 | 默认值 | 说明 |
|---|---|---|---|
rowClassName | string | RowClassName<TRecord> | - | 为 body row 添加 class。 |
customRow | GetComponentProps<TRecord> | - | 为 body row 注入属性、事件和样式。 |
customHeaderRow | (columns: (ColumnType<TRecord> | ColumnGroupType<TRecord>)[], index?: number) => CellAdditionalProps | - | 为 header row 注入属性、事件和样式。 |
ui | SlotProps<{ slots: Record<TableSlots, string> }> | - | 当前表实例的 theme slot class 覆盖。完整 slot 见 ui Slot 参考。 |
Column 字段
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
customRender | (ctx: CustomRenderContext<TRecord>) => VNodeChild | RenderedCell | - | 自定义 body cell 内容。返回 RenderedCell 时可同时设置内容和单元格属性。 |
customCell | GetComponentProps<TRecord, TRecord> | - | 为 body cell 注入属性、事件和样式。 |
customHeaderCell | GetComponentProps<ColumnType<TRecord> | ColumnGroupType<TRecord>, TRecord> | - | 为 header cell 注入属性、事件和样式。 |
Slots
| Slot | 参数类型 | 说明 |
|---|---|---|
bodyCell | TableBodyCellSlotProps<TRecord> | 自定义单元格内容。 |
headerCell | TableHeaderCellSlotProps<TRecord> | 自定义表头单元格内容。 |
summary | () | 自定义摘要区域。 |
empty | () | 自定义空状态。 |
loading | () | 自定义加载态。 |
customFilterDropdown | CustomFilterDropdownSlotProps<TRecord> | 表级自定义筛选面板,列需设置 customFilterDropdown: true。 |
customFilterIcon | { column: ColumnType<TRecord>; filtered: boolean } | 表级自定义筛选图标。 |
paginationItem | TablePaginationSlotProps | 在 pagination.layout 的 { slot: '名称' } 处渲染分页业务内容。 |
主题
自定义插槽提供内容,表格外层结构仍可通过实例 ui 或全局 theme.table.slots 覆盖 class。调整颜色或尺寸时可使用下方 CSS 变量。覆盖顺序见三层主题覆盖。
ui slots
| Slot | 说明 |
|---|---|
headerCellInner | 表头单元格内部的 flex 包裹层。 |
empty | 空状态根容器。 |
emptyWrapper | 空状态内容包裹层。 |
emptyIcon | 空状态图标区域。 |
emptyText | 空状态文字。 |
loading | 覆盖整张表的 loading 遮罩层。 |
loadingSpinner | loading 图标。 |
预设默认值
两套预设的默认 class 可用下方标签切换。默认预编译模式下 utility 的覆盖方式见前缀与覆盖规则。
createVTableGuild({
theme: {
table: {
slots: {
empty: 'text-center text-[color:var(--color-text-disabled)]',
loading: 'absolute inset-0 z-[4] flex items-center justify-center bg-[color:var(--vtg-table-loading-overlay-bg)]',
headerCellInner: "flex min-w-0 items-center [&[data-align=center]]:before:content-[''] [&[data-align=center]]:before:shrink-0 [&[data-align=center]]:before:w-[var(--vtg-header-title-spacer,0px)]",
emptyWrapper: 'my-[var(--vtg-table-empty-margin-block)] flex flex-col items-center justify-center text-center',
emptyIcon: 'mb-[var(--vtg-table-empty-image-margin-bottom)] h-[var(--vtg-table-empty-image-height)] opacity-[var(--vtg-table-empty-image-opacity)] [&>svg]:mx-auto [&>svg]:h-full [&>svg]:w-auto',
emptyText: 'm-0 text-[length:var(--vtg-table-font-size)] leading-[var(--vtg-table-line-height)] text-[color:var(--color-text-disabled)]',
loadingSpinner: 'inline-flex items-center justify-center text-[length:20px] leading-none text-[color:var(--color-primary)]',
},
},
},
})createVTableGuild({
themePreset: 'element-plus',
theme: {
table: {
slots: {
empty: 'py-5 text-center text-[color:var(--color-muted)]',
loading: 'absolute inset-0 z-[2] flex items-center justify-center bg-[color:var(--vtg-table-loading-overlay-bg)]',
headerCellInner: 'flex min-w-0 items-center [&[data-align=center]]:justify-center [&[data-align=right]]:justify-end',
emptyWrapper: 'flex flex-col items-center justify-center',
emptyIcon: 'mb-2',
emptyText: 'm-0 text-[color:var(--color-muted)] text-sm leading-[60px]',
loadingSpinner: 'inline-flex items-center justify-center text-[length:42px] text-[color:var(--color-primary)] animate-spin',
},
},
},
})CSS 变量
表中列出亮色模式的默认值。暗色模式可通过 .dark 单独覆盖。
| 变量 | 作用 | antdv | element-plus |
|---|---|---|---|
--vtg-table-bg | 表格主体背景,对应 table、td 等区域 | var(--color-surface) | var(--color-surface) |
--vtg-table-text-color | 正文文字颜色 | var(--color-on-surface) | var(--color-text-regular) |
--vtg-table-loading-overlay-bg | 加载遮罩层背景 | rgb(255 255 255 / 40%) | rgb(255 255 255 / 90%) |
--vtg-table-empty-margin-block | 空态上下外边距 | 32px | 不读取 |
--vtg-table-empty-image-height | 空态图形高度 | 40px | 不读取 |
--vtg-table-empty-image-margin-bottom | 空态图形与文字间距 | 8px | 不读取 |
--vtg-table-empty-image-opacity | 空态图形透明度 | 1 | 不读取 |
--vtg-table-empty-border-color | antdv 空态插画边框色 | #d9d9d9 | 不读取 |
--vtg-table-empty-shadow-color | antdv 空态插画阴影色 | #f0f0f0 | 不读取 |
--vtg-table-empty-content-color | antdv 空态插画填充色 | #f5f5f5 | 不读取 |
Type Declarations
表级回调属于 TableProps<TRecord>,列级回调属于 ColumnType<TRecord>;所有 Vue 插槽由 TableSlotsDecl<TRecord> 统一声明。CellAdditionalProps 描述属性与鼠标事件的返回值。props / 列配置为相关字段节选;CustomStructureFields 和 MergedColumnFields 是文档辅助接口,不是包导出,完整声明见类型参考,分页插槽完整上下文见分页。
查看类型声明
/** TableProps 节选;文档辅助接口,不是包导出。 */
export interface CustomStructureFields<TRecord extends object = Record<string, any>> {
rowClassName?: string | RowClassName<TRecord>
customRow?: GetComponentProps<TRecord>
customHeaderRow?: (
columns: Array<ColumnType<TRecord> | ColumnGroupType<TRecord>>,
index?: number,
) => CellAdditionalProps
transformCellText?: TransformCellText<TRecord>
}/** ColumnType 节选;文档辅助接口,不是包导出。 */
export interface MergedColumnFields<TRecord extends object = Record<string, any>> {
key?: Key
title?: ColumnTitle<TRecord>
dataIndex?: DataIndex
colSpan?: number
customRender?: (ctx: CustomRenderContext<TRecord>) => VNodeChild | RenderedCell
customCell?: GetComponentProps<TRecord, TRecord>
customHeaderCell?: GetComponentProps<ColumnType<TRecord> | ColumnGroupType<TRecord>, TRecord>
}export type RowClassName<TRecord = Record<string, any>> = (
record: TRecord,
index: number,
indent: number,
) => stringexport type GetComponentProps<TData, TRecord extends object = Record<string, any>> = (
data: TData,
index: number,
column?: ColumnType<TRecord>,
) => CellAdditionalProps | undefinedexport 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
}export interface TableSlotsDecl<TRecord extends object = Record<string, any>> {
bodyCell?: (props: TableBodyCellSlotProps<TRecord>) => VNodeChild
headerCell?: (props: TableHeaderCellSlotProps<TRecord>) => VNodeChild
empty?: () => VNodeChild
loading?: () => VNodeChild
customFilterDropdown?: (props: CustomFilterDropdownSlotProps<TRecord>) => VNodeChild
customFilterIcon?: (props: { column: ColumnType<TRecord>; filtered: boolean }) => VNodeChild
title?: (props: TableDataSlotProps<TRecord>) => VNodeChild
footer?: (props: TableDataSlotProps<TRecord>) => VNodeChild
paginationItem?: (props: TablePaginationSlotProps) => VNodeChild
summary?: () => VNodeChild
expandColumnTitle?: () => VNodeChild
}export interface TableBodyCellSlotProps<TRecord extends object = Record<string, any>> {
text: unknown
record: TRecord
index: number
column: ColumnType<TRecord>
}export interface TableHeaderCellSlotProps<TRecord extends object = Record<string, any>> {
title: VNodeChild | undefined
column: ColumnType<TRecord> | ColumnGroupType<TRecord>
index: number
}export interface TableDataSlotProps<TRecord extends object = Record<string, any>> {
data: TRecord[]
}export interface CustomFilterDropdownSlotProps<TRecord extends object = Record<string, any>> {
column: ColumnType<TRecord>
selectedKeys: (string | number | boolean)[]
setSelectedKeys: (keys: (string | number | boolean)[]) => void
confirm: (options?: { closeDropdown?: boolean }) => void
clearFilters: (options?: { confirm?: boolean; closeDropdown?: boolean }) => void
filters: ColumnFilterItem[]
visible: boolean
close: () => void
}