Appearance
Table表格组件
表格组件用于展示行列数据,适用于大量结构化数据的展现,以及对数据进行排序、搜索、分页、自定义操作等复杂行为。
如何使用
通过指定dataSource(一个数组)和columns来创建表格,示例如下:
html
<template>
<a-table :dataSource="dataSource" :columns="columns" />
</template>
<script>
export default {
setup() {
return {
dataSource: [
{
key: '1',
name: '胡彦斌',
age: 32,
address: '西湖区湖底公园1号',
},
{
key: '2',
name: '胡彦祖',
age: 42,
address: '西湖区湖底公园1号',
},
],
columns: [
{
title: '姓名',
dataIndex: 'name',
key: 'name',
},
{
title: '年龄',
dataIndex: 'age',
key: 'age',
},
{
title: '住址',
dataIndex: 'address',
key: 'address',
},
],
};
},
};
</script>代码演示
- 基本用法:展示简单表格,最后一列包含各种操作。通过
#headerCell和#bodyCell插槽自定义表头和表格单元格内容。
| Name | Age | Address | Tags | Action |
|---|---|---|---|---|
| John Brown | 32 | New York No. 1 Lake Park | NICEDEVELOPER | Invite 一 John BrownDelete More actions |
| Jim Green | 42 | London No. 1 Lake Park | LOSER | Invite 一 Jim GreenDelete More actions |
| Joe Black | 32 | Sidney No. 1 Lake Park | COOLTEACHER | Invite 一 Joe BlackDelete More actions |
vue
<template>
<a-table :columns="columns" :data-source="data">
<template #headerCell="{ column }">
<template v-if="column.key === 'name'">
<span>
<smile-outlined />
Name
</span>
</template>
</template>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'name'">
<a>
{{ record.name }}
</a>
</template>
<template v-else-if="column.key === 'tags'">
<span>
<a-tag
v-for="tag in record.tags"
:key="tag"
:color="tag === 'loser' ? 'volcano' : tag.length > 5 ? 'geekblue' : 'green'"
>
{{ tag.toUpperCase() }}
</a-tag>
</span>
</template>
<template v-else-if="column.key === 'action'">
<span>
<a>Invite 一 {{ record.name }}</a>
<a-divider type="vertical" />
<a>Delete</a>
<a-divider type="vertical" />
<a class="ant-dropdown-link">
More actions
<down-outlined />
</a>
</span>
</template>
</template>
</a-table>
</template>
<script lang="ts" setup>
import { SmileOutlined, DownOutlined } from '@ant-design/icons-vue';
const columns = [
{
name: 'Name',
dataIndex: 'name',
key: 'name',
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
},
{
title: 'Address',
dataIndex: 'address',
key: 'address',
},
{
title: 'Tags',
key: 'tags',
dataIndex: 'tags',
},
{
title: 'Action',
key: 'action',
},
];
const data = [
{
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
tags: ['nice', 'developer'],
},
{
key: '2',
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park',
tags: ['loser'],
},
{
key: '3',
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
tags: ['cool', 'teacher'],
},
];
</script>- 树形数据展示:表格支持树形数据展示,当数据中有
children字段时自动展示为树形表格,可通过childrenColumnName配置树形结构列名,通过indentSize控制缩进宽度。
CheckStrictly:
| Name | Age | Address | |
|---|---|---|---|
| John Brown sr. | 60 | New York No. 1 Lake Park | |
| Joe Black | 32 | Sidney No. 1 Lake Park |
vue
<template>
<a-space align="center" style="margin-bottom: 16px">
CheckStrictly:
<a-switch v-model:checked="rowSelection.checkStrictly"></a-switch>
</a-space>
<a-table :columns="columns" :data-source="data" :row-selection="rowSelection" />
</template>
<script lang="ts" setup>
import { ref } from 'vue';
const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
width: '12%',
},
{
title: 'Address',
dataIndex: 'address',
width: '30%',
key: 'address',
},
];
interface DataItem {
key: number;
name: string;
age: number;
address: string;
children?: DataItem[];
}
const data: DataItem[] = [
{
key: 1,
name: 'John Brown sr.',
age: 60,
address: 'New York No. 1 Lake Park',
children: [
{
key: 11,
name: 'John Brown',
age: 42,
address: 'New York No. 2 Lake Park',
},
{
key: 12,
name: 'John Brown jr.',
age: 30,
address: 'New York No. 3 Lake Park',
children: [
{
key: 121,
name: 'Jimmy Brown',
age: 16,
address: 'New York No. 3 Lake Park',
},
],
},
{
key: 13,
name: 'Jim Green sr.',
age: 72,
address: 'London No. 1 Lake Park',
children: [
{
key: 131,
name: 'Jim Green',
age: 42,
address: 'London No. 2 Lake Park',
children: [
{
key: 1311,
name: 'Jim Green jr.',
age: 25,
address: 'London No. 3 Lake Park',
},
{
key: 1312,
name: 'Jimmy Green sr.',
age: 18,
address: 'London No. 4 Lake Park',
},
],
},
],
},
],
},
{
key: 2,
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
},
];
const rowSelection = ref({
checkStrictly: false,
onChange: (selectedRowKeys: (string | number)[], selectedRows: DataItem[]) => {
console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
},
onSelect: (record: DataItem, selected: boolean, selectedRows: DataItem[]) => {
console.log(record, selected, selectedRows);
},
onSelectAll: (selected: boolean, selectedRows: DataItem[], changeRows: DataItem[]) => {
console.log(selected, selectedRows, changeRows);
},
});
</script>- 表头分组:通过
columns[n]内嵌children渲染分组表头。
| John Brown | 1 | Lake Park | C | 2035 | Lake Street 42 | SoftLake Co | M |
| John Brown | 2 | Lake Park | C | 2035 | Lake Street 42 | SoftLake Co | M |
| John Brown | 3 | Lake Park | C | 2035 | Lake Street 42 | SoftLake Co | M |
| John Brown | 4 | Lake Park | C | 2035 | Lake Street 42 | SoftLake Co | M |
| John Brown | 5 | Lake Park | C | 2035 | Lake Street 42 | SoftLake Co | M |
| John Brown | 6 | Lake Park | C | 2035 | Lake Street 42 | SoftLake Co | M |
| John Brown | 7 | Lake Park | C | 2035 | Lake Street 42 | SoftLake Co | M |
| John Brown | 8 | Lake Park | C | 2035 | Lake Street 42 | SoftLake Co | M |
| John Brown | 9 | Lake Park | C | 2035 | Lake Street 42 | SoftLake Co | M |
| John Brown | 10 | Lake Park | C | 2035 | Lake Street 42 | SoftLake Co | M |
vue
<template>
<a-table
:columns="columns"
:data-source="data"
bordered
size="middle"
:scroll="{ x: 'calc(700px + 50%)', y: 240 }"
/>
</template>
<script lang="ts" setup>
import type { TableColumnsType } from '@design-middle-center/fuse-design-vue'
type TableDataType = {
key: number
name: string
age: number
street: string
building: string
number: number
companyAddress: string
companyName: string
gender: string
}
const columns: TableColumnsType = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
width: 100,
fixed: 'left',
filters: [
{
text: 'Joe',
value: 'Joe'
},
{
text: 'John',
value: 'John'
}
],
onFilter: (value: string | number | boolean, record: TableDataType) =>
record.name.indexOf(value as string) === 0
},
{
title: 'Other',
children: [
{
title: 'Age',
dataIndex: 'age',
key: 'age',
width: 200,
sorter: (a: TableDataType, b: TableDataType) => a.age - b.age
},
{
title: 'Address',
children: [
{
title: 'Street',
dataIndex: 'street',
key: 'street',
width: 200
},
{
title: 'Block',
children: [
{
title: 'Building',
dataIndex: 'building',
key: 'building',
width: 100
},
{
title: 'Door No.',
dataIndex: 'number',
key: 'number',
width: 100
}
]
}
]
}
]
},
{
title: 'Company',
children: [
{
title: 'Company Address',
dataIndex: 'companyAddress',
key: 'companyAddress',
width: 200
},
{
title: 'Company Name',
dataIndex: 'companyName',
key: 'companyName'
}
]
},
{
title: 'Gender',
dataIndex: 'gender',
key: 'gender',
width: 80,
fixed: 'right'
}
]
const data = [...Array(100)].map((_, i) => ({
key: i,
name: 'John Brown',
age: i + 1,
street: 'Lake Park',
building: 'C',
number: 2035,
companyAddress: 'Lake Street 42',
companyName: 'SoftLake Co',
gender: 'M'
}))
</script>API
- Table
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| bodyCell | 个性化单元格 | v-slot:bodyCell="{text, record, index, column}" | - | 3.0 |
| bordered | 是否展示外边框和列边框 | boolean | false | - |
| childrenColumnName | 指定树形结构的列名 | string | children | - |
| columns | 表格列的配置描述 | array | - | - |
| components | 覆盖默认的table元素 | object | - | - |
| customFilterDropdown | 自定义筛选菜单,需配合column.customFilterDropdown使用 | v-slot:customFilterDropdown="FilterDropdownProps " | - | 3.0 |
| customFilterIcon | 自定义筛选图标 | v-slot:customFilterIcon="{filtered, column}" | - | 3.0 |
| customHeaderRow | 设置头部行属性 | Function(columns, index) | - | - |
| customRow | 设置行属性 | Function(record, index) | - | - |
| dataSource | 数据数组 | object[] | - | - |
| defaultExpandAllRows | 初始时是否展开所有行 | boolean | false | - |
| defaultExpandedRowKeys | 默认展开的行 | string[] | - | - |
| emptyText | 自定义空数据时的显示内容 | v-slot:emptyText | - | 3.0 |
| expandedRowKeys(v-model) | 展开的行,控制属性 | string[] | - | - |
| expandedRowRender | 额外的展开行 | Function(record, index, indent, expanded):VNode | v-slot:expandedRowRender="{record, index, indent, expanded}" | - | - |
| expandFixed | 控制展开图标是否固定,可选true、left、right | boolean | string | false | 3.0 |
| expandColumnTitle | 自定义展开列表头 | v-slot | - | 4.0.0 |
| expandIcon | 自定义展开图标 | Function(props):VNode | v-slot:expandIcon="props" | - | - |
| expandRowByClick | 通过点击行来展开子行 | boolean | false | - |
| footer | 表格尾部 | Function(currentPageData)|v-slot:footer="currentPageData" | - | - |
| getPopupContainer | 设置表格内各类浮层的渲染节点,如筛选菜单 | (triggerNode) => HTMLElement | () => TableHtmlElement | 1.5.0 |
| headerCell | 个性化头部单元格 | v-slot:headerCell="{title, column}" | - | 3.0 |
| indentSize | 展示树形数据时,每层缩进的宽度,单位为px | number | 15 | - |
| loading | 页面是否加载中 | boolean|object | false | - |
| locale | 默认文案设置,包括排序、过滤、空数据文案 | object | filterConfirm: 确定filterReset: 重置emptyText: 暂无数据 | - |
| pagination | 分页器,设为false时不展示和进行分页 | object | false | - | - |
| rowClassName | 表格行的类名 | Function(record, index):string | - | - |
| rowExpandable | 设置是否允许行展开 | (record) => boolean | - | 3.0 |
| rowKey | 表格行key的取值,可以是字符串或一个函数 | string | Function(record):string | 'key' |
| rowSelection | 列表项是否可选择的配置项 | object | null | - |
| scroll | 表格是否可滚动,也可指定滚动区域的宽、高 | object | - | - |
| showExpandColumn | 设置是否展示行展开列 | boolean | true | 3.0 |
| showHeader | 是否显示表头 | boolean | true | - |
| showSorterTooltip | 表头是否显示下一次排序的tooltip提示,参数类型为对象时将被设置为Tooltip的属性 | boolean | Tooltip props | true | 3.0 |
| size | 表格大小 | large | middle | small | large | - |
| sortDirections | 支持的排序方式 | Array | ['ascend', 'descend'] | - |
| sticky | 设置粘性头部和滚动条 | boolean | {offsetHeader?: number, offsetScroll?: number, getContainer?: () => HTMLElement} | - | 3.0 |
| summary | 总结栏 | v-slot:summary | - | 3.0 |
| tableLayout | 表格元素的table-layout属性 | - | 'auto' | 'fixed' | 无固定表头/列或使用column.ellipsis时,默认值为fixed | 1.5.0 |
| title | 表格标题 | Function(currentPageData)|v-slot:title="currentPageData" | - | - |
| transformCellText | 数据渲染前再次改变数据,一般用于空数据的默认配置,可通过ConfigProvider全局统一配置 | Function({ text, column, record, index }) => any | - | 1.5.4 |
- 事件
| 事件名称 | 说明 | 回调参数 |
|---|---|---|
| change | 分页、排序、筛选变化时触发 | Function(pagination, filters, sorter, { action, currentDataSource }) |
| expand | 点击展开图标时触发 | Function(expanded, record) |
| expandedRowsChange | 展开的行变化时触发 | Function(expandedRows) |
| resizeColumn | 拖动列时触发 | Function(width, column) |
- Column:列描述数据对象,是
columns中的一项
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| align | 设置列的对齐方式 | left | right | center | left | - |
| colSpan | 表头列合并,设置为0时不渲染 | number | - | - |
| customCell | 设置单元格属性 | Function(record, rowIndex, column) | - | column add from 3.0 |
| customFilterDropdown | 启用v-slot:customFilterDropdown,优先级低于filterDropdown | boolean | false | 3.0 |
| customHeaderCell | 设置头部单元格属性 | Function(column) | - | - |
| customRender | 生成复杂数据的渲染函数 | Function({text, record, index, column}) {} | - | - |
| dataIndex | 列数据在数据项中对应的路径,支持通过数组查询嵌套路径 | string | string[] | - | - |
| defaultFilteredValue | 默认筛选值 | string[] | - | 1.5.0 |
| filterResetToDefaultFilteredValue | 点击重置按钮时,是否恢复默认筛选值 | boolean | false | 3.3.0 |
| defaultSortOrder | 默认排序顺序 | ascend | descend | - | - |
| ellipsis | 超过宽度自动省略,暂不支持与排序筛选一起使用,设置为true或{ showTitle?: boolean }时,表格布局变为tableLayout="fixed" | boolean | { showTitle?: boolean } | false | 3.0 |
| filterDropdown | 自定义筛选菜单 | VNode | (props: FilterDropdownProps) => VNode | - | - |
| filterDropdownOpen | 控制自定义筛选菜单是否可见 | boolean | - | - |
| filtered | 标识数据是否经过过滤,筛选图标会高亮 | boolean | false | - |
| filteredValue | 筛选的受控属性,外界可用此控制列的筛选状态 | string[] | - | - |
| filterIcon | 自定义filter图标 | VNode | ({filtered: boolean, column: Column}) => vNode | false | - |
| filterMode | 指定筛选菜单的用户界面 | 'menu' | 'tree' | 'menu' | 3.0 |
| filterMultiple | 是否多选 | boolean | true | - |
| filters | 表头的筛选菜单项 | object[] | - | - |
| filterSearch | 筛选菜单项是否可搜索 | boolean | function(input, filter):boolean | false | boolean:3.0 function:3.3.0 |
| fixed | 列是否固定,可选true(等效于left)、'left'、'right' | boolean|string | false | - |
| key | Vue需要的key | string | - | - |
| maxWidth | 拖动列最大宽度 | number | - | 3.0 |
| minWidth | 拖动列最小宽度 | number | 50 | 3.0 |
| resizable | 是否可拖动调整宽度 | boolean | - | 3.0 |
| responsive | 响应式breakpoint配置列表 | Breakpoint [] | - | 3.0 |
| rowScope | 设置列范围 | row | rowgroup | - | 4.0 |
| showSorterTooltip | 表头显示下一次排序的tooltip提示,覆盖table中的showSorterTooltip | boolean | Tooltip props | true | - |
| sortDirections | 支持的排序方式 | Array | ['ascend', 'descend'] | 1.5.0 |
| sorter | 排序函数 | Function | boolean | - |
| sortOrder | 排序的受控属性 | string | - | - |
| title | 列头显示文字 | string | - | - |
| width | 列宽度 | string | number | - |
| onFilter | 本地模式下,确定筛选的运行函数 | Function | - | - |
| onFilterDropdownOpenChange | 自定义筛选菜单可见变化时调用 | function(open) {} | - | 4.0 |
- ColumnGroup
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| title | 列头显示文字 | string | slot |
- pagination:分页的配置项
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| position | 指定分页显示的位置 | Array | ['bottomRight'] |
- rowSelection:选择功能的配置
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| checkStrictly | checkable状态下节点选择完全受控 | boolean | true | 3.0 |
| columnTitle | 自定义列表选择框标题 | string | VNode | - |
| columnWidth | 自定义列表选择框宽度 | string | number | - |
| fixed | 把选择框列固定在左边 | boolean | - | - |
| getCheckboxProps | 选择框的默认属性配置 | Function(record) | - | - |
| hideDefaultSelections | 去掉『全选』『反选』两个默认选项 | boolean | false | - |
| hideSelectAll | 隐藏全选勾选框与自定义选择项 | boolean | false | 3.0 |
| preserveSelectedRowKeys | 数据删除时保留选项的key | boolean | - | 3.0 |
| selectedRowKeys | 指定选中项的key数组 | string[] | [] | - |
| selections | 自定义选择项配置项,设为true时使用默认选择项 | object[] | boolean | true |
| type | 多选/单选 | string | checkbox | - |
| onChange | 选中项发生变化时的回调 | Function(selectedRowKeys, selectedRows) | - | - |
| onSelect | 用户手动选择/取消选择某列的回调 | Function(record, selected, selectedRows, nativeEvent) | - | - |
| onSelectAll | 用户手动选择/取消选择所有列的回调 | Function(selected, selectedRows, changeRows) | - | - |
| onSelectInvert | 用户手动选择反选的回调 | Function(selectedRows) | - | - |
| onSelectNone | 用户清空选择的回调 | function() | - | 3.0 |
- scroll
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| scrollToFirstRowOnChange | 分页、排序、筛选变化后是否滚动到表格顶部 | boolean | - |
| x | 设置横向滚动 | string | number |
| y | 设置纵向滚动 | string | number |
- selection:自定义选择配置项
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| key | Vue需要的key | string | - |
| text | 选择项显示的文字 | string | VNode |
| onSelect | 选择项点击回调 | Function(changeableRowKeys) | - |
- FilterDropdownProps
typescript
interface FilterDropdownProps {
prefixCls: string;
setSelectedKeys: (selectedKeys: Key[]) => void;
selectedKeys: Key[];
confirm: (param?: FilterConfirmProps) => void;
clearFilters?: (param?: FilterResetProps) => void;
filters?: ColumnFilterItem[];
visible: boolean;
column: ColumnType;
}
interface FilterConfirmProps {
closeDropdown: boolean;
}
interface FilterResetProps {
confirm?: boolean;
closeDropdown?: boolean;
}注意
在Table中,dataSource和columns里的数据值都需指定key值。若dataSource数据没有key属性,务必使用rowKey指定数据列的主键,否则控制台会提示缺少key,表格组件也可能出现错误。
FAQ(续)
- 表格过滤时会回到第一页?:前端过滤时,由于条目总数减少,可能导致总页数小于筛选前的当前页数。为防止当前页面无数据,默认会返回第一页。若使用远程分页且需要保持当前页面,可手动控制当前页面不变。
- 表格分页为何会出现size切换器?:自
3.0起,Pagination在total大于50条时,默认显示size切换器以提升用户交互体验。若不需要该功能,可通过设置showSizeChanger为false来关闭。 - 固定列穿透到最上层该怎么办?:固定列通过
z - index属性悬浮于非固定列之上,当在Table上放置遮罩层时,可能出现固定列被透过的情况。为遮罩层设置更高的z - index值,即可覆盖固定列。

