Skip to content

展开行 ​

展开行适合在不跳转详情页的前提下,直接在当前表格里展示补充信息。它和树形表格不是同一类能力:树形表格处理的是数据天然分层,展开行处理的是“当前行需要额外展开一块内容”。

在线示例 ​

defaultExpandedRowKeys 预展开第一行;第三行被 rowExpandable 判定为不可展开,不显示展开图标。

在 Playground 中编辑

基础示例 ​

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,完整字段与回调签名见页末声明。

展开列位置与标题 ​

expandable.expandIconColumnIndex 指定展开列在业务列中的位置,从 0 开始;选择列仍保留在前。负数和越界值会收敛到有效范围。显式的 EXPAND_COLUMN 占位优先于该位置配置。

expandable.columnTitle 支持文本或数字标题。复杂内容使用 #expandColumnTitle,其优先级高于 columnTitle;选择列的标题仍由 rowSelection.columnTitle 配置。树形缩进图标也可使用 expandIconColumnIndex 选择业务列。

什么时候用 ​

  • 行内详情预览
  • 补充说明、标签、备注信息
  • 内容明显超出单元格承载范围,但又不值得单独跳转详情页

已知限制 ​

  • 不支持 preserve-expanded-content:收起行时,展开内容会从 DOM 中销毁,再次展开时重新渲染。element-plus 原版 Table(2.9.7+)提供了 preserve-expanded-content 属性来保留已展开内容的 DOM 状态,当前 vtable-guild 尚未实现该能力。如果展开内容包含表单或有状态组件,收起后状态会丢失。

API ​

TRecord 表示业务行类型。表格的完整配置见 API Reference。

Props ​

Prop类型默认值说明
expandableExpandable<TRecord>-开启展开行能力。
rowKeystring | ((record: TRecord) => Key)'key'行唯一标识。选择状态按行 key 记录,必须稳定且唯一。

Expandable ​

字段类型默认值说明
expandedRowRender(record: TRecord, index: number, indent: number, expanded: boolean) => VNodeChild-展开行内容渲染函数。
expandedRowKeysKey[]-受控展开行 key。
defaultExpandedRowKeysKey[]-默认展开行 key。
expandRowByClickbooleanfalse点击整行展开。
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-展开 key 变化回调。
columnWidthnumber | string-展开列宽度。
fixed'left' | 'right' | true-展开列固定位置;true 等同 'left'。
defaultExpandAllRowsbooleanfalse默认展开所有行。
rowExpandable(record: TRecord) => boolean-判断某行是否可展开。
showExpandColumnbooleantrue是否显示展开列。
expandIconColumnIndexnumber0展开列在业务列中的位置,从零开始。显式的 EXPAND_COLUMN 占位优先。
columnTitlestring | number''展开列标题,可由 #expandColumnTitle 覆盖。
expandedRowClassNamestring | RowClassName<TRecord>-展开行 class。

Slots ​

Slot参数类型说明
expandColumnTitle()自定义展开列标题。

主题 ​

展开内容与展开按钮的 class 可通过实例 ui 或全局 theme.table.slots 覆盖。调整颜色或尺寸时可使用下方 CSS 变量。覆盖顺序见三层主题覆盖。

ui slots ​

Slot说明
expandIcon展开按钮基础样式。
expandIconExpanded展开状态。
expandIconCollapsed折叠状态。
expandIconSpaced不可展开行的占位符。
expandIconDisabled禁用状态。
expandIconSymbol展开图标的 symbol 容器。
expandIconSymbolExpanded展开 symbol 状态。
expandIconSymbolCollapsed折叠 symbol 状态。
expandedRow展开行 <tr>。
expandedRowCell展开内容 <td>。

预设默认值 ​

两套预设的默认 class 可用下方标签切换。默认预编译模式下 utility 的覆盖方式见前缀与覆盖规则。

ts
createVTableGuild({
  theme: {
    table: {
      slots: {
        expandIcon: "relative inline-flex h-[17px] w-[17px] shrink-0 items-center justify-center origin-center scale-[0.941176] p-0 leading-[17px] rounded-[6px] border border-[color:var(--vtg-table-border-color,#d9d9d9)] bg-[color:var(--vtg-table-bg,#fff)] text-[color:var(--vtg-table-text-color)] cursor-pointer align-sub select-none transition-[border-color,background-color,color] duration-200 hover:border-[color:var(--color-primary)] hover:text-[color:var(--color-primary)] focus:border-[color:var(--color-primary)] focus:text-[color:var(--color-primary)] active:border-[color:var(--color-primary)] active:text-[color:var(--color-primary)] before:absolute before:left-[3px] before:right-[3px] before:top-[7px] before:h-px before:bg-current before:content-[''] after:absolute after:bottom-[3px] after:left-[7px] after:top-[3px] after:w-px after:origin-center after:bg-current after:content-[''] after:transition-transform after:duration-200",
        expandIconExpanded: 'after:scale-y-0',
        expandIconCollapsed: 'after:scale-y-100',
        expandIconSpaced: 'cursor-default border-transparent bg-transparent before:hidden after:hidden invisible',
        expandIconDisabled: 'cursor-default border-transparent bg-transparent before:hidden after:hidden invisible',
        expandIconSymbol: 'hidden',
        expandIconSymbolExpanded: '',
        expandIconSymbolCollapsed: '',
        expandedRow: '',
        expandedRowCell: 'bg-[color:var(--vtg-table-expanded-row-bg,#fafafa)]',
      },
    },
  },
})
ts
createVTableGuild({
  themePreset: 'element-plus',
  theme: {
    table: {
      slots: {
        expandIcon: 'inline-flex h-[23px] w-[23px] shrink-0 items-center justify-center border-0 bg-transparent p-0 text-[color:var(--vtg-table-text-color)] cursor-pointer align-middle transition-[color,transform] duration-200',
        expandIconExpanded: '',
        expandIconCollapsed: '',
        expandIconSpaced: 'cursor-default opacity-0',
        expandIconDisabled: 'cursor-not-allowed text-[color:var(--color-muted)]',
        expandIconSymbol: 'inline-flex items-center justify-center text-[12px] leading-none transition-transform duration-300 [&>svg]:h-[1em] [&>svg]:w-[1em]',
        expandIconSymbolExpanded: 'rotate-90',
        expandIconSymbolCollapsed: 'rotate-0',
        expandedRow: '',
        expandedRowCell: 'bg-[color:var(--vtg-table-expanded-row-bg,#fafafa)]',
      },
    },
  },
})

CSS 变量 ​

表中列出亮色模式的默认值。暗色模式可通过 .dark 单独覆盖。

变量作用antdvelement-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-border-color表格边框和单元格分隔线var(--color-default)var(--color-default)
--color-primary主题主色,用于拖拽手柄悬停、按钮高亮和字段状态等#1677ff#409eff
--vtg-table-expanded-row-bg展开行内容背景未声明,回退 #fafafa未声明,回退 #fafafa

Type Declarations ​

以下声明包含展开状态、详情渲染、图标参数和展开回调。VNodeChild 是 Vue 的可渲染内容类型;其他依赖类型见类型参考。

查看类型声明
ts
export type Key = string | number
ts
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
  expandIconColumnIndex?: number
  columnTitle?: string | number
  expandedRowClassName?: string | RowClassName<TRecord>
}
ts
export type RowClassName<TRecord = Record<string, any>> = (
  record: TRecord,
  index: number,
  indent: number,
) => string

相关页面 ​