Skip to content

ProTable 高级表格 ​

设计原则:配置 + 插槽,绝不做黑盒。 列由 props 声明,单元格由插槽渲染,你需要 el-table-column 的任何原生能力都能透传。

最简用法 ​

vue
<script setup lang="ts">
import type { ProTableColumn } from '@/components/ProTable/types'

const columns: ProTableColumn[] = [
  { label: 'ID', prop: 'id', width: 90 },
  { label: '名称', prop: 'name', minWidth: 160 },
  { label: '状态', prop: 'status', slot: 'status', width: 100 },
  { label: '操作', prop: 'operation', slot: 'operation', width: 180, fixed: 'right' }
]

async function request(params: Record<string, any>) {
  const res = await myApi.list(params)
  return { list: res.list ?? [], total: res.total ?? 0 }
}
</script>

<template>
  <ProTable :columns="columns" :request="request" row-key="id">
    <template #status="{ row }">
      <DictTag :value="row.status" dict-type="sys_normal_disable" />
    </template>
    <template #operation="{ row }">
      <el-button link type="primary" @click="openEdit(row)">编辑</el-button>
    </template>
  </ProTable>
</template>

props ​

prop类型默认说明
columnsProTableColumn[]必填列配置
request(params) => Promise<{list,total}>必填数据源,组件负责分页参数
rowKeystringid行唯一键
selectionbooleanfalse是否显示多选列
showIndexbooleantrue是否显示序号列
stripebooleantrue斑马纹
borderbooleanfalse边框
heightstring | number—固定表头高度
expandAllbooleanfalse树表默认全展开
childrenFieldstring—树表子节点字段(如 children)
showRefreshbooleantrue显示刷新按钮
exportNamestring—传了才显示导出按钮,值作为文件名
printTitlestring—传了才显示打印按钮
initParamsobject—首次查询的附加参数

列配置 ​

ts
interface ProTableColumn {
  label: string
  prop: string
  width?: number | string
  minWidth?: number | string
  align?: 'left' | 'center' | 'right'
  sortable?: boolean | 'custom'
  fixed?: 'left' | 'right' | boolean
  hidden?: boolean          // 默认隐藏,列设置里可勾回来
  slot?: string             // 渲染该列用的插槽名
  formatter?: (row: any) => string
  children?: ProTableColumn[]   // 多级表头
}

暴露的方法(ref) ​

ts
const tableRef = ref()
tableRef.value.reload()                 // 回到第一页重新查
tableRef.value.refresh()                // 保持当前页重新查
tableRef.value.getSelection()           // 当前选中行
tableRef.value.exportExcel()            // 手动触发导出
tableRef.value.clearSelection()

事件 ​

事件参数说明
selection-changerows多选变化
row-clickrow行点击
sort-change{ prop, order }排序变化(需自行把参数拼进 request)

列设置 ​

工具栏右侧的齿轮按钮打开列设置:勾选显隐、上下移动调序、设置左右固定。用户拖出来的顺序与显隐不会自动持久化到 localStorage——需要持久化的话监听 change 事件自己存。

树形表格 ​

vue
<ProTable :columns="columns" :request="request" row-key="deptId" children-field="children" />

后端返回嵌套 children 即可,不必拍平。

内嵌工具栏 ​

vue
<ProTable :columns="columns" :request="request" export-name="用户列表.xlsx">
  <template #toolbar-left>
    <el-button type="primary" @click="openAdd">新增</el-button>
    <el-button type="danger" :disabled="!selected.length">批量删除</el-button>
  </template>
  <template #toolbar-right>
    <el-button @click="doImport">导入</el-button>
  </template>
</ProTable>