ProTable 声明式数据表格
ProTable(src/components/pro/ProTable.vue)将中后台列表页高频组合的**「顶部声明式搜索栏 + 卡片标题与工具栏 + 紧凑/标准密度切换 + 列显隐设置(支持持久化)+ 表格全屏 + 批量选择栏 + VbTable + VbPagination」**整合为单一高阶组件。
基础用法
只需传入 columns、searchFields 与一个符合 { total, items } 分页契约的 request 函数,即可开箱获得搜索、远程排序、分页与工具栏能力:
vue
<script setup lang="ts">
import { ref } from 'vue'
import { fetchUsers } from '@/api/system'
import { ProTable } from '@/components/pro'
import type { ProSearchField, ProTableColumn } from '@/components/pro'
const tableRef = ref<InstanceType<typeof ProTable> | null>(null)
const searchFields: ProSearchField[] = [
{ prop: 'query', label: '关键词', placeholder: '用户名 / 昵称 / 邮箱' },
{
prop: 'is_active',
label: '状态',
type: 'select',
options: [
{ label: '启用中', value: 'true' },
{ label: '已停用', value: 'false' },
],
},
]
const columns: ProTableColumn[] = [
{ key: 'username', title: '账号 / 昵称', sortable: true },
{ key: 'department', title: '部门' },
{ key: 'is_active', title: '状态', align: 'center', width: 100 },
{ key: 'actions', title: '操作', align: 'right', width: 140 },
]
</script>
<template>
<ProTable
ref="tableRef"
table-key="system-users"
title="用户管理"
subtitle="连接 /api/system/users 实时分页与 RBAC 权限控制"
:columns="columns"
:search-fields="searchFields"
:request="fetchUsers"
selectable
>
<template #toolbar>
<VbButton type="primary" size="sm" @click="openCreate">新增用户</VbButton>
</template>
<template #batch-actions="{ keys }">
<VbButton type="soft" size="sm">批量导出 ({{ keys.length }})</VbButton>
</template>
<template #cell-actions="{ row }">
<VbButton type="ghost" size="sm">编辑 {{ row.username }}</VbButton>
</template>
</ProTable>
</template>搜索字段类型(ProSearchField)
type | 渲染组件 | 参数映射行为 |
|---|---|---|
'input'(默认) | VbInput | 自动 .trim(),空串不提交 |
'select' | VbSelect | 配合 options 渲染下拉筛选 |
'tree-select' | VbTreeSelect | 配合 treeData 渲染树形筛选(如部门子树筛选) |
'date' | VbDatePicker | 单日期字符串 |
'date-range' | VbDatePicker (range) | 自动拆分为 <prop>_start 与 <prop>_end 两个查询参数 |
Props
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
title / subtitle | string | — | 表格外层 VbCard 主标题与副标题 |
tableKey | string | — | 唯一标识;传入后列显隐偏好自动持久化至 localStorage(键 vobo:pro-table:cols:<key>) |
columns | ProTableColumn[] | 必填 | 列定义,完整兼容 VbTable 的 TableColumn(含 align、width、sortable 等) |
request | (params) => Promise<{ total, items }> | 必填 | 异步拉取数据函数 |
searchFields | ProSearchField[] | [] | 顶部搜索表单字段配置;为空时不渲染搜索卡片 |
rowKey | string | 'id' | 行主键字段名 |
selectable | boolean | false | 是否开启多选列及顶部批量操作提示条 |
expandable | boolean | false | 是否开启展开行(配合 #expand 插槽) |
stripe / bordered | boolean | true / false | 斑马纹与外边框开关 |
defaultPageSize | number | 10 | 默认每页条数 |
immediate | boolean | true | 挂载后是否立即触发首次请求 |
插槽(Slots)
#toolbar:卡片右上角自定义主操作区(位于密度/列设置/全屏/刷新按钮左侧)#batch-actions="{ keys, rows }":多选后出现在柔和翠绿批量条中的批量操作按钮#cell-<key>/#header-<key>/#expand/#summary:自动透传至底层VbTable
Events & 暴露方法(defineExpose)
- Event:
@selection-change="(keys, rows) => void" reload(resetPage = false):重新请求数据,resetPage = true时回到第 1 页reset():重置全部搜索条件、排序与选中状态并刷新clearSelection():清空当前多选行resetColumns():恢复全部列的显示状态getQueryParams():返回当前生效的搜索、排序与分页参数(常用于透传给审计日志 CSV 导出)selectedKeys/rows/total:只读响应式状态引用