Table表格
Table 表格
展示行列数据。
设计师专属
安装 Kitchen Sketch 插件 💎,两步就可以自动生成 Ant Design 表格组件。
何时使用
-
当有大量结构化的数据需要展现时;
-
当需要对数据进行排序、搜索、分页、自定义操作等复杂行为时。
如何使用
指定表格的数据源 dataSource 为一个数组。
<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>
代码演示
基本用法
简单的表格,最后一列是各种操作。
<template> <a-table :columns="columns" :data-source="data"> <template #name="{ text }"> <a>{{ text }}</a> </template> <template #customTitle> <span> <smile-outlined /> Name </span> </template> <template #tags="{ text: tags }"> <span> <a-tag v-for="tag in tags" :key="tag" :color="tag === 'loser' ? 'volcano' : tag.length > 5 ? 'geekblue' : 'green'" > {{ tag.toUpperCase() }} </a-tag> </span> </template> <template #action="{ record }"> <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> </a-table></template><script lang="ts">import { SmileOutlined, DownOutlined } from '@ant-design/icons-vue';import { defineComponent } from 'vue';const columns = [ { dataIndex: 'name', key: 'name', slots: { title: 'customTitle', customRender: 'name' }, }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, { title: 'Tags', key: 'tags', dataIndex: 'tags', slots: { customRender: 'tags' }, }, { title: 'Action', key: 'action', slots: { customRender: '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'], },];export default defineComponent({ setup() { return { data, columns, }; }, components: { SmileOutlined, DownOutlined, },});</script>
远程加载数据
这个例子通过简单的 ajax 读取方式,演示了如何从服务端读取并展现数据,具有筛选、排序等功能以及页面 loading 效果。开发者可以自行接入其他数据处理方式。
另外,本例也展示了筛选排序功能如何交给服务端实现,列不需要指定具体的 onFilter 和 sorter 函数,而是在把筛选和排序的参数发到服务端来处理。
<template> <a-table :columns="columns" :row-key="record => record.login.uuid" :data-source="dataSource" :pagination="pagination" :loading="loading" @change="handleTableChange" > <template #name="{ text }">{{ text.first }} {{ text.last }}</template> </a-table></template><script lang="ts">import { TableState, TableStateFilters } from 'ant-design-vue/es/table/interface';import { useRequest } from 'vue-request';import axios from 'axios';import { defineComponent, reactive, UnwrapRef } from 'vue';const columns = [ { title: 'Name', dataIndex: 'name', sorter: true, width: '20%', slots: { customRender: 'name' }, }, { title: 'Gender', dataIndex: 'gender', filters: [ { text: 'Male', value: 'male' }, { text: 'Female', value: 'female' }, ], width: '20%', }, { title: 'Email', dataIndex: 'email', },];type Pagination = TableState['pagination'];type APIParams = { results: number; page?: number; sortField?: string; sortOrder?: number; [key: string]: any;};type APIResult = { results: { gender: 'female' | 'male'; name: string; email: string; }[];};const queryData = (params: APIParams) => { return axios.get<APIResult>('https://randomuser.me/api', { params: params });};export default defineComponent({ setup() { const pagination: UnwrapRef<Pagination> = reactive({ total: 200 }); const { data: dataSource, run, loading } = useRequest(queryData, { defaultParams: [ { results: 10, }, ], formatResult: res => res.data.results, }); const handleTableChange = (pag: Pagination, filters: TableStateFilters, sorter: any) => { console.log(pag); Object.assign(pagination, pag); run({ results: pagination.pageSize!, page: pagination.current, sortField: sorter.field, sortOrder: sorter.order, ...filters, }); }; return { dataSource, pagination, loading, columns, handleTableChange, }; },});</script>
带边框
添加表格边框线,页头和页脚。
<template> <a-table :columns="columns" :data-source="data" bordered> <template #name="{ text }"> <a>{{ text }}</a> </template> <template #title>Header</template> <template #footer>Footer</template> </a-table></template><script lang="ts">import { defineComponent } from 'vue';const columns = [ { title: 'Name', dataIndex: 'name', slots: { customRender: 'name' }, }, { title: 'Cash Assets', className: 'column-money', dataIndex: 'money', }, { title: 'Address', dataIndex: 'address', },];const data = [ { key: '1', name: 'John Brown', money: '¥300,000.00', address: 'New York No. 1 Lake Park', }, { key: '2', name: 'Jim Green', money: '¥1,256,000.00', address: 'London No. 1 Lake Park', }, { key: '3', name: 'Joe Black', money: '¥120,000.00', address: 'Sidney No. 1 Lake Park', },];export default defineComponent({ setup() { return { data, columns, }; },});</script><style>th.column-money,td.column-money { text-align: right !important;}</style>
单元格自动省略
设置 column.ellipsis 可以让单元格内容根据宽度自动省略。
列头缩略暂不支持和排序筛选一起使用。
<template> <a-table :columns="columns" :data-source="data"> <template #name="{ text }"> <a>{{ text }}</a> </template> </a-table></template><script lang="ts">import { defineComponent } from 'vue';const columns = [ { title: 'Name', dataIndex: 'name', key: 'name', slots: { customRender: 'name' }, }, { title: 'Age', dataIndex: 'age', key: 'age', width: 80, }, { title: 'Address', dataIndex: 'address', key: 'address 1', ellipsis: true, }, { title: 'Long Column Long Column Long Column', dataIndex: 'address', key: 'address 2', ellipsis: true, }, { title: 'Long Column Long Column', dataIndex: 'address', key: 'address 3', ellipsis: true, }, { title: 'Long Column', dataIndex: 'address', key: 'address 4', ellipsis: true, },];const data = [ { key: '1', name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park, New York No. 1 Lake Park', tags: ['nice', 'developer'], }, { key: '2', name: 'Jim Green', age: 42, address: 'London No. 2 Lake Park, London No. 2 Lake Park', tags: ['loser'], }, { key: '3', name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park, Sidney No. 1 Lake Park', tags: ['cool', 'teacher'], },];export default defineComponent({ setup() { return { data, columns, }; },});</script>
表格行/列合并
表头只支持列合并,使用 column 里的 colSpan 进行设置。 表格支持行/列合并,使用 render 里的单元格属性 colSpan 或者 rowSpan 设值为 0 时,设置的表格不会渲染。
