Skip to content

ProTable 高级表格

基本使用

网络请求

多级表头

展开行

可编辑行

内置渲染器

Props

属性说明类型默认值
headerTitle工具栏左侧表头string | slot
options工具栏配置, 可根据需要具体显示TableOptions | booleantrue
columnsState列状态配置ColumnsState{}
columns列配置ProTableColumns[]
data静态数据源Array[]
request数据请求函数(...rest: any[]) => Promise<any[]>
params数据请求参数object{}
transform数据请求响应结果处理器(response: any) => { data: any[]; total: number }
transformParams数据请求参数处理器,处理最终 params 与内部参数合并后的请求参数(params: any) => any
pagination分页相关配置ProTablePaginationConfigboolean
checkablecheckbox 列开关booleanfalse
selectedKeys/v-model:selectedKeyscheckbox 列选中的 rowKeyArray[]
loading/v-model: loading数据加载状态booleanfalse
editable编辑模式下配置ProTableEditable{}
cacheSelectedData分页数据下 选中项回显需要添加,用于分页选中回显状态。Array[]

columns 配置

属性说明类型可选值默认值
title标题number | string
key唯一标识, 同时作为 slot 注册向外暴露number |string
tip标题 tips 提示
valueType内置渲染器类型ValueTypetext
valueEnum数值枚举ValueEnum
children同自身配置
render自定义渲染函数,支持组件渲染,功能同 slot(scope: ProTableSlotScope <T>) => number | string | undefined | null | VNode | Component
editable是否可编辑boolean
rowComponent编辑列组件配置ProTableEditRowComponent

Type ProTableColumnItem

ts
export interface ProTableColumnItem<T = any>
  extends Partial<Omit<TableColumnCtx<T>, 'children' | 'label' | 'prop'>> {
  title?: number | string
  key: ColumnKey
  /** 创建一个提示图标 */
  tip?: string
  /** 当前数值类型 default: text */
  valueType?: ValueType
  /** 数值枚举 */
  valueEnum?: ValueEnum
  /** 是否可编辑 */
  editable?: boolean
  /** 编辑列组件配置 */
  rowComponent?: ProTableEditRowComponent
  /** 子集表头 */
  children?: ProTableColumnItem<T>[]
  /** 函数式渲染器 优先级小于 slot */
  render?: (scope: ProTableSlotScope<T>) => number | string | undefined | null | VNode | Component
}

export interface ProTableEditRowComponent {
  el: Component | string
  props?: Record<string, any>
  rules?: EditRowRule[]
}

export interface EditRowRule {
  required?: boolean
  message?: string
  validator?: (value: any, row: any, callback: (error?: string | Error) => void) => void
}

columnsState 列状态配置

属性说明类型可选值默认值
persistenceKey持久化 keystring
persistenceType持久化类型stringlocalStorage| sessionStoragelocalStorage
value列状态值ColumnsMap{}
change列状态变更回调(map: ColumnsMap) => void

pagination 配置

属性说明类型可选值默认值
page页码number1
pageSize大小number10
layout分页器布局string[]同 el-pagination
pageSizes同 el-paginationnumber[]
background是否背景色booleantrue

pagination 默认配置

ts
const DEFAULT_PAGINATON_CONFIG = {
  pageSizes: [10, 20, 30, 40],
  background: true,
  layout: ['total', 'sizes', 'prev', 'pager', 'next', 'jumper']
}

Type ProTablePaginationConfig

ts
export interface ProTablePaginationConfig {
  page?: number
  pageSize?: number
  layout?: string[]
  pageSizes?: number[]
  background?: boolean
}

editable 配置

属性说明类型可选值默认值
mode单行编辑模式,多行编辑模式stringsingle | multiplesingle
onSave保存回调,需 调用 next 函数(row: any, next: () => void) => void
onCancel取消编辑回调,需 调用 next 函数(row: any, next: () => void) => void
onDelete删除回调,需 调用 next 函数(row: any, next: () => void) => void
onChange数据变更回调 ,onSave | onDelete 执行过后(data: any[]) => void
onError验证错误回调,返回当前行验证的所有错误信息(errors: EditableCellValidError | undefined) => void

Type ProTableEditable

ts
/** 编辑表格配置 */
export interface ProTableEditable {
  mode?: 'single' | 'multiple'
  onSave?: (row: any, next: () => void) => void
  onCancel?: (row: any, next: () => void) => void
  onDelete?: (row: any, next: () => void) => void
  onChange?: (data: any[]) => void
  onError?: (errors: EditableCellValidError | undefined) => void
}
export type EditableCellValidError = Record<string, { message: string }>