Table表格·续3
<template> <a-table :columns="columns" :data-source="data" :row-selection="rowSelection" /></template><script lang="ts">import { defineComponent } 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 = { 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); },};export default defineComponent({ setup() { return { data, columns, rowSelection, }; },});</script>
可展开
当表格内容较多不能一次性完全展示时。
<template> <a-table :columns="columns" :data-source="data"> <template> <a>Delete</a> </template> <template #expandedRowRender="{ record }"> <p style="margin: 0"> {{ record.description }} </p> </template> </a-table></template><script lang="ts">import { defineComponent } from 'vue';const columns = [ { title: 'Name', dataIndex: 'name', key: 'name' }, { title: 'Age', dataIndex: 'age', key: 'age' }, { title: 'Address', dataIndex: 'address', key: 'address' }, { title: 'Action', dataIndex: '', key: 'x', slots: { customRender: 'action' } },];const data = [ { key: 1, name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', description: 'My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park.', }, { key: 2, name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park', description: 'My name is Jim Green, I am 42 years old, living in London No. 1 Lake Park.', }, { key: 3, name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park', description: 'My name is Joe Black, I am 32 years old, living in Sidney No. 1 Lake Park.', },];export default defineComponent({ setup() { return { data, columns, }; },});</script>
固定头和列
适合同时展示有大量数据和数据列。
若列头与内容不对齐或出现列重复,请指定固定列的宽度 width。如果指定 width 不生效或出现白色垂直空隙,请尝试建议留一列不设宽度以适应弹性布局,或者检查是否有超长连续字段破坏布局。
建议指定 scroll.x 为大于表格宽度的固定值或百分比。注意,且非固定列宽度之和不要超过 scroll.x。
<template> <a-table :columns="columns" :data-source="data" :scroll="{ x: 1500, y: 300 }"> <template #action> <a>action</a> </template> </a-table></template><script lang="ts">import { defineComponent } from 'vue';const columns = [ { title: 'Full Name', width: 100, dataIndex: 'name', key: 'name', fixed: 'left' }, { title: 'Age', width: 100, dataIndex: 'age', key: 'age', fixed: 'left' }, { title: 'Column 1', dataIndex: 'address', key: '1', width: 150 }, { title: 'Column 2', dataIndex: 'address', key: '2', width: 150 }, { title: 'Column 3', dataIndex: 'address', key: '3', width: 150 }, { title: 'Column 4', dataIndex: 'address', key: '4', width: 150 }, { title: 'Column 5', dataIndex: 'address', key: '5', width: 150 }, { title: 'Column 6', dataIndex: 'address', key: '6', width: 150 }, { title: 'Column 7', dataIndex: 'address', key: '7', width: 150 }, { title: 'Column 8', dataIndex: 'address', key: '8' }, { title: 'Action', key: 'operation', fixed: 'right', width: 100, slots: { customRender: 'action' }, },];interface DataItem { key: number; name: string; age: number; address: string;}const data: DataItem[] = [];for (let i = 0; i < 100; i++) { data.push({ key: i, name: `Edrward ${i}`, age: 32, address: `London Park no. ${i}`, });}export default defineComponent({ data() { return { data, columns, }; },});</script>
固定列
对于列数很多的数据,可以固定前后的列,横向滚动查看其它数据,需要和 scroll.x 配合使用。
若列头与内容不对齐或出现列重复,请指定固定列的宽度 width。如果指定 width 不生效或出现白色垂直空隙,请尝试建议留一列不设宽度以适应弹性布局,或者检查是否有超长连续字段破坏布局。
建议指定 scroll.x 为大于表格宽度的固定值或百分比。注意,且非固定列宽度之和不要超过 scroll.x。
<template> <a-table :columns="columns" :data-source="data" :scroll="{ x: 1300 }"> <template #action> <a>action</a> </template> </a-table></template><script lang="ts">import { defineComponent } from 'vue';const columns = [ { title: 'Full Name', width: 100, dataIndex: 'name', key: 'name', fixed: 'left' }, { title: 'Age', width: 100, dataIndex: 'age', key: 'age', fixed: 'left' }, { title: 'Column 1', dataIndex: 'address', key: '1' }, { title: 'Column 2', dataIndex: 'address', key: '2' }, { title: 'Column 3', dataIndex: 'address', key: '3' }, { title: 'Column 4', dataIndex: 'address', key: '4' }, { title: 'Column 5', dataIndex: 'address', key: '5' }, { title: 'Column 6', dataIndex: 'address', key: '6' }, { title: 'Column 7', dataIndex: 'address', key: '7' }, { title: 'Column 8', dataIndex: 'address', key: '8' }, { title: 'Action', key: 'operation', fixed: 'right', width: 100, slots: { customRender: 'action' }, },];interface DataItem { key: string; name: string; age: number; address: string;}const data: DataItem[] = [ { key: '1', name: 'John Brown', age: 32, address: 'New York Park', }, { key: '2', name: 'Jim Green', age: 40, address: 'London Park', },];export default defineComponent({ data() { return { data, columns, }; },});</script>
固定表头
方便一页内展示大量数据。
需要指定 column 的 width 属性,否则列头和内容可能不对齐。如果指定 width 不生效或出现白色垂直空隙,请尝试建议留一列不设宽度以适应弹性布局,或者检查是否有超长连续字段破坏布局。
<template> <a-table :columns="columns" :data-source="data" :pagination="{ pageSize: 50 }" :scroll="{ y: 240 }" /></template><script lang="ts">import { defineComponent } from 'vue';const columns = [ { title: 'Name', dataIndex: 'name', width: 150, }, { title: 'Age', dataIndex: 'age', width: 150, }, { title: 'Address', dataIndex: 'address', },];const data = [...Array(100)].map((_, i) => ({ key: i, name: `Edward King ${i}`, age: 32, address: `London, Park Lane no. ${i}`,}));export default defineComponent({ setup() { return { data, columns, }; },});</script>
表头分组
columns[n] 可以内嵌 children,以渲染分组表头。
