基础用法
| 名称 | 体积 | 更新时间 | |
|---|---|---|---|
| 组件库 | 51 | 1.2 MB | 2026-09-14 |
| 文档站 | 8 | 320 KB | 2026-09-12 |
| 演示页 | 120 | 890 KB | 2026-09-10 |
| 测试用例 | 83 | — | 2026-09-14 |
<script setup>
import { ref } from 'vue'
const rows = ref([
{ name: '组件库', count: 51, size: '1.2 MB' },
{ name: '文档站', count: 8, size: '320 KB' }
])
const columns = [
{ key: 'name', title: '名称' },
{ key: 'count', title: '数量', sortable: true, align: 'right' },
{ key: 'size', title: '体积', align: 'right' }
]
</script>
<template>
<SrTable :data="rows" :columns="columns" />
</template>表格是数据驱动的,没有 SrTableColumn
这里最容易踩坑
SrTable 没有子组件。列由 columns 数组定义,而不是写成 <SrTableColumn> 标签。
<!-- ❌ 这样写,Vue 会把它当成未注册的原生标签 -->
<SrTable :data="rows">
<SrTableColumn prop="name" label="名称" />
</SrTable>
<!-- ✅ 正确 -->
<SrTable :data="rows" :columns="columns" />写错的后果很隐蔽:页面照常渲染,只是所有单元格都是空的, 控制台只有一条 Failed to resolve component 警告。
SrTableColumn 这个名字确实存在,但它是 types.ts 里的类型,不是组件。
列配置
| 字段 | 类型 | 说明 |
|---|---|---|
key | string | 取值字段名,支持 a.b 嵌套路径 |
title | string | 列标题 |
width | string | 列宽('120px' / '20%') |
minWidth | string | 内容挤压时的下限 |
align | 'left' | 'center' | 'right' | 对齐方式 |
sortable | boolean | 是否可排序 |
fixed | 'left' | 'right' | 固定列 |
render | (row, value) => string | 列级文本渲染,用于日期 / 金额等格式化 |
插槽
列只负责「取哪个字段」。要往单元格里放元素,必须用插槽。
| 插槽名 | 作用域 | 说明 |
|---|---|---|
cell-{key} | { row, column, value, index } | 定制单元格内容 |
header-{key} | { column } | 定制表头内容 |
{key} 就是列定义里的 key。
<SrTable :data="rows" :columns="columns">
<!-- 单元格:标签、按钮、进度条 -->
<template #cell-status="{ value }">
<SrTag :type="value === 'active' ? 'success' : 'default'">{{ value }}</SrTag>
</template>
<!-- 表头:加图标或说明 -->
<template #header-name="{ column }">
{{ column.title }} ⓘ
</template>
</SrTable>覆盖表头会连带丢掉排序控件
header-{key} 替换的是整个表头内容,而排序按钮就在默认内容里。
给 sortable 的列写了 header-{key} 之后,那一列就不能点了——需要自己把 @click 到 toggleSort 的逻辑补回去,或者干脆不覆盖表头。
render 只能返回文本
返回值会被 String() 转换。往里 return 组件不会报错,但单元格会显示成 [object Object]。
要插入元素请用 cell-{key} 插槽。
排序
点表头切换升序 / 降序 / 无排序(三态循环)。
| 名称 | 体积 | 更新时间 | |
|---|---|---|---|
| 文档站 | 8 | 320 KB | 2026-09-12 |
| 组件库 | 51 | 1.2 MB | 2026-09-14 |
| 测试用例 | 83 | — | 2026-09-14 |
| 演示页 | 120 | 890 KB | 2026-09-10 |
<SrTable :data="rows" :columns="columns" default-sort-key="count" default-sort-order="asc" />数字按数值比较,不是字典序
normalizeValue 会判断字段类型:两个都是数字时走数值比较, 其余情况降级为字符串比较。
统一转字符串会让 10 排在 9 前面——这是表格排序最常见的错误。 点上面「数量」列头验证:结果应该是 8 → 51 → 83 → 120, 而不是字典序的 120 → 51 → 83 → 8。
排序是在组件内部对传入数据做副本排序,不会改动 props.data。
// 组件内部:复制后再排
return [...props.data].sort(...)直接 sort() 会就地修改数组——宿主的数据也会跟着变, 这类 bug 极难追查(数据「自己」变了)。
固定列与横向滚动
fixed 用 position: sticky 实现。左侧固定列须在列序最前, 右侧固定列须在最后。
| 名称 | 体积 | 更新时间 | 操作 | |
|---|---|---|---|---|
| 组件库 | 51 | 1.2 MB | 2026-09-14 | |
| 文档站 | 8 | 320 KB | 2026-09-12 | |
| 演示页 | 120 | 890 KB | 2026-09-10 | |
| 测试用例 | 83 | — | 2026-09-14 |
const columns = [
{ key: 'name', title: '名称', width: '160px', fixed: 'left' },
{ key: 'count', title: '数量', width: '200px' },
{ key: 'updated', title: '更新时间', width: '200px' },
{ key: 'action', title: '操作', width: '120px', fixed: 'right' }
]固定列需要显式宽度
sticky 的偏移量靠累计前面各列的宽度算出。 没有宽度的列会被内容撑开,累计值随之失准,固定列的位置就会错。
给所有固定列、以及它们前面的每一列都写上 width。
吸顶表头
sticky-header 配合 max-height,滚动时表头保持可见。
| 名称 | 体积 | 更新时间 | |
|---|---|---|---|
| 组件库 | 51 | 1.2 MB | 2026-09-14 |
| 文档站 | 8 | 320 KB | 2026-09-12 |
| 演示页 | 120 | 890 KB | 2026-09-10 |
| 测试用例 | 83 | — | 2026-09-14 |
<SrTable :data="rows" :columns="columns" sticky-header max-height="160px" />条纹、描边与悬停
| 名称 | 体积 | 更新时间 | |
|---|---|---|---|
| 组件库 | 51 | 1.2 MB | 2026-09-14 |
| 文档站 | 8 | 320 KB | 2026-09-12 |
| 演示页 | 120 | 890 KB | 2026-09-10 |
| 测试用例 | 83 | — | 2026-09-14 |
<SrTable :data="rows" :columns="columns" striped bordered highlight-hover />透明变体
variant="plain" 去掉单元格底色,让表格沉进父级容器:
| 名称 | 体积 | 更新时间 | |
|---|---|---|---|
| 组件库 | 51 | 1.2 MB | 2026-09-14 |
| 文档站 | 8 | 320 KB | 2026-09-12 |
| 演示页 | 120 | 890 KB | 2026-09-10 |
| 测试用例 | 83 | — | 2026-09-14 |
<SrTable :data="rows" :columns="columns" variant="plain" />放进玻璃卡片、面板这类半透明容器时用它。默认底色 --sr-color-surface 是 80% 不透明白, 嵌在磨砂背景里会像「卡里贴了张纸」——这是实际使用中反馈最多的一处。
两件事不会被一起改掉
- 行反馈仍然生效:
highlight-hover/striped的选择器特异性更高,plain压不住它们 - 吸顶表头会自动补回底色:透明表头一旦 sticky,滚动时下层内容会从表头穿过去。 这是
plain下唯一不透明的地方,属于必要妥协
选择列
selectable 打开选择列,选中项用 v-model:selected-keys 绑定(存的是行 key,不是行对象)。
| 名称 | 体积 | 更新时间 | ||
|---|---|---|---|---|
| 组件库 | 51 | 1.2 MB | 2026-09-14 | |
| 文档站 | 8 | 320 KB | 2026-09-12 | |
| 演示页 | 120 | 890 KB | 2026-09-10 | |
| 测试用例 | 83 | — | 2026-09-14 |
已选 0 行:(无)
<script setup>
const selected = ref<string[]>([]) // 行 key 数组
</script>
<template>
<SrTable
v-model:selected-keys="selected"
:data="rows"
:columns="columns"
row-key="id"
selectable
@selection-change="(keys, rows) => console.log(keys, rows)"
/>
</template>三个需要知道的行为
1. 跨页保持由宿主负责。
组件只增删 key,不会因为 data 变少而清空。翻页时把同一个数组传进来,上一页的选择自然还在:
<!-- 翻页只换 data,selected 保持不变 -->
<SrTable v-model:selected-keys="selected" :data="pageData" :columns="columns" selectable />2. 全选只作用于当前页可勾选的行。
否则翻页后点一次「全选」,上一页的勾选就被一起清掉了——用户会觉得「选了半天全没了」。
3. selection-change 的 rows 只含当前 data 里能解析到的行。
跨页残留的 key 在当前数据里查不到,不会凭空构造行对象给你。
禁用某些行
<SrTable selectable :row-selectable="(row) => row.status !== 'archived'" />禁用行不渲染勾选框,并且不参与全选与半选的计算——否则会出现「显示已全选,但列表里还有几行没勾」的矛盾状态。
空态与加载
loading 时显示骨架行,行数由 loadingRows 控制—— 骨架的宽度应该与实际内容接近,否则数据到位时会有明显的跳动。
| 名称 | 体积 | 更新时间 | |
|---|---|---|---|
还没有数据 | |||
| 名称 | 体积 | 更新时间 | |
|---|---|---|---|
<SrTable :data="[]" :columns="columns" empty-text="还没有数据" />
<SrTable :data="[]" :columns="columns" loading :loading-rows="3" />尺寸
| 名称 | 体积 | 更新时间 | |
|---|---|---|---|
| 组件库 | 51 | 1.2 MB | 2026-09-14 |
| 文档站 | 8 | 320 KB | 2026-09-12 |
<SrTable size="sm" :data="rows" :columns="columns" />
<SrTable size="md" :data="rows" :columns="columns" />Props
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
data | Record<string, any>[] | [] | 表格数据 |
columns | SrTableColumn[] | [] | 列配置 |
rowKey | string | — | 行唯一标识字段,用于 v-for 的 key |
striped | boolean | false | 斑马纹 |
bordered | boolean | false | 是否显示描边 |
highlightHover | boolean | true | 悬停高亮行 |
size | 'sm' | 'md' | 'lg' | 'md' | 尺寸 |
variant | 'default' | 'plain' | 'default' | 外观变体,plain 去掉单元格底色 |
selectable | boolean | false | 是否显示选择列 |
rowSelectable | (row, index) => boolean | — | 单行是否可勾选,返回 false 时不渲染勾选框 |
emptyText | string | — | 空态文案 |
loading | boolean | false | 是否显示骨架 |
loadingRows | number | 5 | 骨架行数 |
stickyHeader | boolean | false | 表头吸顶 |
maxHeight | string | — | 最大高度,配合 stickyHeader 使用 |
defaultSortKey | string | — | 初始排序列 |
defaultSortOrder | 'asc' | 'desc' | — | 初始排序方向 |
事件
| 事件 | 参数 | 说明 |
|---|---|---|
sort-change | { key, order } | 排序变化,order 为 'asc' / 'desc' / null |
row-click | (row, index) | 点击某一行 |
update:selected-keys | (keys) | 选择变化,配合 v-model:selected-keys |
selection-change | (keys, rows) | 选择变化,额外回传当前页能解析到的行 |
无障碍
- 语义化标签:
<table>/<thead>/<tbody>/<th>,读屏软件能按表格朗读 - 可排序列的
<th>带aria-sort(ascending/descending/none), 这是唯一能让读屏用户知道当前排序状态的方式 - 骨架屏带
aria-busy="true",避免有辅助技术把占位内容当成真实数据