Table表格·续2
<template> <a-button class="editable-add-btn" @click="handleAdd" style="margin-bottom: 8px">Add</a-button> <a-table bordered :data-source="dataSource" :columns="columns"> <template #name="{ text, record }"> <div class="editable-cell"> <div v-if="editableData[record.key]" class="editable-cell-input-wrapper"> <a-input v-model:value="editableData[record.key].name" @pressEnter="save(record.key)" /> <check-outlined class="editable-cell-icon-check" @click="save(record.key)" /> </div> <div v-else class="editable-cell-text-wrapper"> {{ text || ' ' }} <edit-outlined class="editable-cell-icon" @click="edit(record.key)" /> </div> </div> </template> <template #operation="{ record }"> <a-popconfirm v-if="dataSource.length" title="Sure to delete?" @confirm="onDelete(record.key)" > <a>Delete</a> </a-popconfirm> </template> </a-table></template><script lang="ts">import { computed, defineComponent, reactive, Ref, ref, UnwrapRef } from 'vue';import { CheckOutlined, EditOutlined } from '@ant-design/icons-vue';import { cloneDeep } from 'lodash-es';interface DataItem { key: string; name: string; age: number; address: string;}export default defineComponent({ components: { CheckOutlined, EditOutlined, }, setup() { const columns = [ { title: 'name', dataIndex: 'name', width: '30%', slots: { customRender: 'name' }, }, { title: 'age', dataIndex: 'age', }, { title: 'address', dataIndex: 'address', }, { title: 'operation', dataIndex: 'operation', slots: { customRender: 'operation' }, }, ]; const dataSource: Ref<DataItem[]> = ref([ { key: '0', name: 'Edward King 0', age: 32, address: 'London, Park Lane no. 0', }, { key: '1', name: 'Edward King 1', age: 32, address: 'London, Park Lane no. 1', }, ]); const count = computed(() => dataSource.value.length + 1); const editableData: UnwrapRef<Record<string, DataItem>> = reactive({}); const edit = (key: string) => { editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.key)[0]); }; const save = (key: string) => { Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]); delete editableData[key]; }; const onDelete = (key: string) => { dataSource.value = dataSource.value.filter(item => item.key !== key); }; const handleAdd = () => { const newData = { key: `${count.value}`, name: `Edward King ${count.value}`, age: 32, address: `London, Park Lane no. ${count.value}`, }; dataSource.value.push(newData); }; return { columns, onDelete, handleAdd, dataSource, editableData, count, edit, save, }; },});</script><style lang="less">.editable-cell { position: relative; .editable-cell-input-wrapper, .editable-cell-text-wrapper { padding-right: 24px; } .editable-cell-text-wrapper { padding: 5px 24px 5px 5px; } .editable-cell-icon, .editable-cell-icon-check { position: absolute; right: 0; width: 20px; cursor: pointer; } .editable-cell-icon { margin-top: 4px; display: none; } .editable-cell-icon-check { line-height: 28px; } .editable-cell-icon:hover, .editable-cell-icon-check:hover { color: #108ee9; } .editable-add-btn { margin-bottom: 8px; }}.editable-cell:hover .editable-cell-icon { display: inline-block;}</style>
可编辑行
带行编辑功能的表格。
<template> <a-table :columns="columns" :data-source="dataSource" bordered> <template v-for="col in ['name', 'age', 'address']" #[col]="{ text, record }" :key="col"> <div> <a-input v-if="editableData[record.key]" v-model:value="editableData[record.key][col]" style="margin: -5px 0" /> <template v-else> {{ text }} </template> </div> </template> <template #operation="{ record }"> <div class="editable-row-operations"> <span v-if="editableData[record.key]"> <a @click="save(record.key)">Save</a> <a-popconfirm title="Sure to cancel?" @confirm="cancel(record.key)"> <a>Cancel</a> </a-popconfirm> </span> <span v-else> <a @click="edit(record.key)">Edit</a> </span> </div> </template> </a-table></template><script lang="ts">import { cloneDeep } from 'lodash-es';import { defineComponent, reactive, ref, UnwrapRef } from 'vue';const columns = [ { title: 'name', dataIndex: 'name', width: '25%', slots: { customRender: 'name' }, }, { title: 'age', dataIndex: 'age', width: '15%', slots: { customRender: 'age' }, }, { title: 'address', dataIndex: 'address', width: '40%', slots: { customRender: 'address' }, }, { title: 'operation', dataIndex: 'operation', slots: { customRender: 'operation' }, },];interface DataItem { key: string; name: string; age: number; address: string;}const data: DataItem[] = [];for (let i = 0; i < 100; i++) { data.push({ key: i.toString(), name: `Edrward ${i}`, age: 32, address: `London Park no. ${i}`, });}export default defineComponent({ setup() { const dataSource = ref(data); const editableData: UnwrapRef<Record<string, DataItem>> = reactive({}); const edit = (key: string) => { editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.key)[0]); }; const save = (key: string) => { Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]); delete editableData[key]; }; const cancel = (key: string) => { delete editableData[key]; }; return { dataSource, columns, editingKey: '', editableData, edit, save, cancel, }; },});</script><style scoped>.editable-row-operations a { margin-right: 8px;}</style>
树形数据展示
表格支持树形数据的展示,当数据中有 children 字段时会自动展示为树形表格,如果不需要或配置为其他字段可以用 childrenColumnName 进行配置。 可以通过设置 indentSize 以控制每一层的缩进宽度。
注:暂不支持父子数据递归关联选择。
