展开行
展开行适合在不跳转详情页的前提下,直接在当前表格里展示补充信息。它和树形表格不是同一类能力:树形表格处理的是数据天然分层,展开行处理的是“当前行需要额外展开一块内容”。
在线示例
defaultExpandedRowKeys 预展开第一行;第三行被 rowExpandable 判定为不可展开,不显示展开图标。
vue
<script setup lang="ts">
import { h } from 'vue'
import { VTable } from '@vtable-guild/vtable-guild'
import type { Expandable, TableColumnsType } from '@vtable-guild/vtable-guild'
interface OrderRow {
key: string
no: string
customer: string
amount: number
note: string
refundable: boolean
}
const columns: TableColumnsType<OrderRow> = [
{ title: '订单号', dataIndex: 'no', key: 'no' },
{ title: '客户', dataIndex: 'customer', key: 'customer' },
{
title: '金额',
dataIndex: 'amount',
key: 'amount',
align: 'right',
customRender: ({ text }) => `¥ ${Number(text).toLocaleString()}`,
},
]
const dataSource: OrderRow[] = [
{
key: '1',
no: 'SO-20260712-001',
customer: '云图科技',
amount: 128400,
note: '客户要求分两批发货,第二批等对方仓库腾空后再排期。',
refundable: true,
},
{
key: '2',
no: 'SO-20260712-002',
customer: '衡石数据',
amount: 42600,
note: '已开票,发票号 25017788。',
refundable: true,
},
{
key: '3',
no: 'SO-20260713-004',
customer: '南岭制造',
amount: 9800,
note: '(此行不可展开)',
refundable: false,
},
]
const expandable: Expandable<OrderRow> = {
defaultExpandedRowKeys: ['1'],
// rowExpandable 返回 false 的行不显示展开图标
rowExpandable: (record) => record.refundable,
expandedRowRender: (record) =>
h('div', { style: 'padding: 4px 0; font-size: 13px; line-height: 1.7' }, [
h('div', `备注:${record.note}`),
h('div', { style: 'opacity: 0.65' }, `可退款:${record.refundable ? '是' : '否'}`),
]),
}
</script>
<template>
<VTable row-key="key" :columns="columns" :data-source="dataSource" :expandable="expandable" />
</template>基础示例
vue
<script setup lang="ts">
import { VTable, type TableColumnsType, type Expandable } from '@vtable-guild/vtable-guild'
interface UserRow {
key: string
name: string
role: string
city: string
}
const columns: TableColumnsType<UserRow> = [{ title: '姓名', dataIndex: 'name', key: 'name' }]
const expandable: Expandable<UserRow> = {
expandRowByClick: true,
expandedRowRender: (record) => `${record.name} 负责 ${record.role},所在城市为 ${record.city}`,
}
const dataSource: UserRow[] = [{ key: '1', name: 'Ada', role: '产品设计', city: 'Shanghai' }]
</script>
<template>
<VTable row-key="key" :columns="columns" :data-source="dataSource" :expandable="expandable" />
</template>常见扩展点
expandRowByClick整行点击展开rowExpandable: (record: TRecord) => boolean按业务条件决定哪些行可展开expandIcon: NonNullable<Expandable<TRecord>['expandIcon']>自定义展开图标showExpandColumn: false隐藏独立展开列,只保留行点击展开
TRecord 由 Expandable<UserRow> 中的 UserRow 指定;展开图标的参数对象包含 expanded、record、expandable 和 onExpand,完整字段与回调签名见页末声明。
什么时候用
- 行内详情预览
- 补充说明、标签、备注信息
- 内容明显超出单元格承载范围,但又不值得单独跳转详情页
已知限制
- 不支持 preserve-expanded-content:收起行时,展开内容会从 DOM 中销毁,再次展开时重新渲染。element-plus 原版 Table(2.9.7+)提供了
preserve-expanded-content属性来保留已展开内容的 DOM 状态,当前 vtable-guild 尚未实现该能力。如果展开内容包含表单或有状态组件,收起后状态会丢失。
相关页面
Type Declarations
以下声明包含展开状态、详情渲染、图标参数和展开回调。VNodeChild 是 Vue 的可渲染内容类型;其他依赖类型见类型参考。
查看类型声明
ts
export type Key = string | numberts
export interface Expandable<TRecord extends object = Record<string, any>> {
expandedRowRender?: (
record: TRecord,
index: number,
indent: number,
expanded: boolean,
) => VNodeChild
expandedRowKeys?: Key[]
defaultExpandedRowKeys?: Key[]
expandRowByClick?: boolean
expandIcon?: (props: {
expanded: boolean
record: TRecord
expandable: boolean
onExpand: (record: TRecord, e: Event) => void
}) => VNodeChild
onExpand?: (expanded: boolean, record: TRecord) => void
onExpandedRowsChange?: (expandedKeys: Key[]) => void
columnWidth?: number | string
fixed?: 'left' | 'right' | true
defaultExpandAllRows?: boolean
rowExpandable?: (record: TRecord) => boolean
showExpandColumn?: boolean
expandedRowClassName?: string | RowClassName<TRecord>
}ts
export type RowClassName<TRecord = Record<string, any>> = (
record: TRecord,
index: number,
indent: number,
) => string