Table表格·续5
<template> <a-table :columns="columns" :data-source="data" class="components-table-demo-nested"> <template #operation> <a>Publish</a> </template> <template #expandedRowRender> <a-table :columns="innerColumns" :data-source="innerData" :pagination="false"> <template #status> <span> <a-badge status="success" /> Finished </span> </template> <template #operation> <span class="table-operation"> <a>Pause</a> <a>Stop</a> <a-dropdown> <template #overlay> <a-menu> <a-menu-item>Action 1</a-menu-item> <a-menu-item>Action 2</a-menu-item> </a-menu> </template> <a> More <down-outlined /> </a> </a-dropdown> </span> </template> </a-table> </template> </a-table></template><script lang="ts">import { DownOutlined } from '@ant-design/icons-vue';import { defineComponent } from 'vue';const columns = [ { title: 'Name', dataIndex: 'name', key: 'name' }, { title: 'Platform', dataIndex: 'platform', key: 'platform' }, { title: 'Version', dataIndex: 'version', key: 'version' }, { title: 'Upgraded', dataIndex: 'upgradeNum', key: 'upgradeNum' }, { title: 'Creator', dataIndex: 'creator', key: 'creator' }, { title: 'Date', dataIndex: 'createdAt', key: 'createdAt' }, { title: 'Action', key: 'operation', slots: { customRender: 'operation' } },];interface DataItem { key: number; name: string; platform: string; version: string; upgradeNum: number; creator: string; createdAt: string;}const data: DataItem[] = [];for (let i = 0; i < 3; ++i) { data.push({ key: i, name: 'Screem', platform: 'iOS', version: '10.3.4.5654', upgradeNum: 500, creator: 'Jack', createdAt: '2014-12-24 23:12:00', });}const innerColumns = [ { title: 'Date', dataIndex: 'date', key: 'date' }, { title: 'Name', dataIndex: 'name', key: 'name' }, { title: 'Status', key: 'state', slots: { customRender: 'status' } }, { title: 'Upgrade Status', dataIndex: 'upgradeNum', key: 'upgradeNum' }, { title: 'Action', dataIndex: 'operation', key: 'operation', slots: { customRender: 'operation' }, },];interface innerDataItem { key: number; date: string; name: string; upgradeNum: string;}const innerData: innerDataItem[] = [];for (let i = 0; i < 3; ++i) { innerData.push({ key: i, date: '2014-12-24 23:12:00', name: 'This is production name', upgradeNum: 'Upgraded: 56', });}export default defineComponent({ components: { DownOutlined, }, setup() { return { data, columns, innerColumns, innerData, }; },});</script>
可控的筛选和排序
使用受控属性对筛选和排序状态进行控制。
-
columns 中定义了 filteredValue 和 sortOrder 属性即视为受控模式。
-
只支持同时对一列进行排序,请保证只有一列的 sortOrder 属性是生效的。
-
务必指定 column.key。
<template> <div> <div class="table-operations"> <a-button @click="setAgeSort">Sort age</a-button> <a-button @click="clearFilters">Clear filters</a-button> <a-button @click="clearAll">Clear filters and sorters</a-button> </div> <a-table :columns="columns" :data-source="data" @change="handleChange" /> </div></template><script lang="ts">import { computed, defineComponent, ref } from 'vue';import { TableState, TableStateFilters } from 'ant-design-vue/es/table/interface';type Pagination = TableState['pagination'];interface DataItem { key: string; name: string; age: number; address: string;}const data: DataItem[] = [ { key: '1', name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', }, { key: '2', name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park', }, { key: '3', name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park', }, { key: '4', name: 'Jim Red', age: 32, address: 'London No. 2 Lake Park', },];export default defineComponent({ setup() { const filteredInfo = ref(); const sortedInfo = ref(); const columns = computed(() => { const filtered = filteredInfo.value || {}; const sorted = sortedInfo.value || {}; return [ { title: 'Name', dataIndex: 'name', key: 'name', filters: [ { text: 'Joe', value: 'Joe' }, { text: 'Jim', value: 'Jim' }, ], filteredValue: filtered.name || null, onFilter: (value: string, record: DataItem) => record.name.includes(value), sorter: (a: DataItem, b: DataItem) => a.name.length - b.name.length, sortOrder: sorted.columnKey === 'name' && sorted.order, ellipsis: true, }, { title: 'Age', dataIndex: 'age', key: 'age', sorter: (a: DataItem, b: DataItem) => a.age - b.age, sortOrder: sorted.columnKey === 'age' && sorted.order, }, { title: 'Address', dataIndex: 'address', key: 'address', filters: [ { text: 'London', value: 'London' }, { text: 'New York', value: 'New York' }, ], filteredValue: filtered.address || null, onFilter: (value: string, record: DataItem) => record.address.includes(value), sorter: (a: DataItem, b: DataItem) => a.address.length - b.address.length, sortOrder: sorted.columnKey === 'address' && sorted.order, ellipsis: true, }, ]; }); const handleChange = (pagination: Pagination, filters: TableStateFilters, sorter: any) => { console.log('Various parameters', pagination, filters, sorter); filteredInfo.value = filters; sortedInfo.value = sorter; }; const clearFilters = () => { filteredInfo.value = null; }; const clearAll = () => { filteredInfo.value = null; sortedInfo.value = null; }; const setAgeSort = () => { sortedInfo.value = { order: 'descend', columnKey: 'age', }; }; return { data, columns, handleChange, clearFilters, clearAll, setAgeSort, }; },});</script><style scoped>.table-operations { margin-bottom: 16px;}.table-operations > button { margin-right: 8px;}</style>
选择和操作
选择后进行操作,完成后清空选择,通过 rowSelection.selectedRowKeys 来控制选中项。
