Vue / 系统课时
封装数据表格
封装数据表格
管理后台中的列表页通常都要处理加载遮罩、空数据、列定义和操作按钮。本课将这些重复逻辑抽到 BaseTable,同时用格式化函数与具名插槽保留业务列的扩展能力。
学习目标
- 划分通用表格与业务页面的职责边界
- 用列配置统一普通字段、格式化字段和自定义单元格
- 通过操作插槽复用编辑、删除等行级动作区域
明确组件边界
通用表格负责展示规则,不负责请求接口。组件接收 data、columns、loading 等状态,统一渲染空数据和操作列;业务页面仍负责查询参数、接口调用、权限判断以及按钮行为。
列配置只描述稳定的展示信息。普通字段直接读取 prop,轻量转换使用 formatter,包含标签、按钮或复杂布局时使用 slot。这样既能减少模板重复,又不会把业务判断塞进基础组件。
实现通用表格
以下为片段示例,依赖项目已安装并注册 Element Plus。新建 src/components/BaseTable.vue:
vue
<script setup lang="ts">
type TableRow = Record<string, unknown>
interface TableColumn {
prop?: string
label: string
width?: number
minWidth?: number
align?: 'left' | 'center' | 'right'
fixed?: boolean | 'left' | 'right'
slot?: string
formatter?: (row: TableRow) => string
}
withDefaults(
defineProps<{
data: TableRow[]
columns: TableColumn[]
loading?: boolean
rowKey?: string | ((row: TableRow) => string | number)
emptyText?: string
showActions?: boolean
actionsLabel?: string
actionsWidth?: number
}>(),
{
loading: false,
rowKey: 'id',
emptyText: '暂无数据',
showActions: true,
actionsLabel: '操作',
actionsWidth: 160
}
)
</script>
<template>
<el-table
v-loading="loading"
:data="data"
:row-key="rowKey"
border
style="width: 100%"
>
<el-table-column
v-for="column in columns"
:key="column.prop ?? column.label"
:prop="column.prop"
:label="column.label"
:width="column.width"
:min-width="column.minWidth"
:align="column.align ?? 'left'"
:fixed="column.fixed"
>
<template #default="{ row, $index }">
<slot
v-if="column.slot"
:name="column.slot"
:row="row"
:index="$index"
>
{{ column.prop ? row[column.prop] : '' }}
</slot>
<span v-else>
{{ column.formatter ? column.formatter(row) : column.prop ? row[column.prop] : '' }}
</span>
</template>
</el-table-column>
<el-table-column
v-if="showActions && $slots.actions"
:label="actionsLabel"
:width="actionsWidth"
fixed="right"
align="center"
>
<template #default="{ row, $index }">
<slot name="actions" :row="row" :index="$index" />
</template>
</el-table-column>
<template #empty>
<el-empty :description="emptyText" />
</template>
</el-table>
</template>预期结果:
text
传入 loading=true 时显示加载遮罩;data 为空时显示“暂无数据”;传入 actions 插槽时自动出现固定在右侧的操作列。在业务页面中扩展列
业务页面只需要声明列配置,并为复杂列提供同名插槽。下面的状态列使用 status 插槽,创建时间则用 formatter 完成轻量格式化:
vue
<script setup lang="ts">
import { ref } from 'vue'
import BaseTable from '@/components/BaseTable.vue'
const loading = ref(false)
const rows = ref([
{ id: 1, name: '张三', status: 'enabled', createdAt: '2026-08-06 10:00:00' },
{ id: 2, name: '李四', status: 'disabled', createdAt: '2026-08-06 10:30:00' }
])
const columns = [
{ prop: 'name', label: '姓名', minWidth: 140 },
{ prop: 'status', label: '状态', width: 100, slot: 'status' },
{
prop: 'createdAt',
label: '创建时间',
minWidth: 180,
formatter: (row: Record<string, unknown>) => String(row.createdAt)
}
]
function editUser(id: unknown) {
console.log('edit user:', id)
}
</script>
<template>
<BaseTable :data="rows" :columns="columns" :loading="loading">
<template #status="{ row }">
<el-tag :type="row.status === 'enabled' ? 'success' : 'info'">
{{ row.status === 'enabled' ? '启用' : '停用' }}
</el-tag>
</template>
<template #actions="{ row }">
<el-button link type="primary" @click="editUser(row.id)">编辑</el-button>
</template>
</BaseTable>
</template>预期结果:
text
页面显示姓名、状态、创建时间和操作四列;状态以标签展示,点击“编辑”后控制台输出对应用户 ID。处理关键边界
rowKey 必须稳定且唯一,否则刷新数据后可能出现选择状态错位。基础组件不要直接内置删除确认、权限码或路由跳转,这些行为依赖具体业务,应继续由操作插槽实现。
当列需要排序、筛选或多选时,可以继续透传 Element Plus 的列属性与表格事件,但应按真实需求增加,避免把所有底层参数一次性暴露成难以维护的万能组件。
按自己的节奏,确认真正理解。