From 35cce66a5f8ffbc47ae07e4ea3a239320737db4c Mon Sep 17 00:00:00 2001 From: frezs Date: Tue, 31 Aug 2021 21:06:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(table):=20=20=E6=B7=BB=E5=8A=A0=E5=92=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8A=A8=E6=80=81=E5=88=A0=E9=99=A4=E5=92=8C?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Table/src/BasicTable.vue | 4 +++ .../Table/src/hooks/useDataSource.ts | 27 +++++++++++++++++++ src/components/Table/src/hooks/useTable.ts | 6 +++++ src/components/Table/src/types/table.ts | 2 ++ 4 files changed, 39 insertions(+) diff --git a/src/components/Table/src/BasicTable.vue b/src/components/Table/src/BasicTable.vue index b584e73d392..1b74089ad76 100644 --- a/src/components/Table/src/BasicTable.vue +++ b/src/components/Table/src/BasicTable.vue @@ -141,6 +141,8 @@ getRawDataSource, setTableData, updateTableDataRecord, + deleteTableDataRecord, + insertTableDataRecord, findTableDataRecord, fetch, getRowKey, @@ -279,6 +281,8 @@ setPagination, setTableData, updateTableDataRecord, + deleteTableDataRecord, + insertTableDataRecord, findTableDataRecord, redoHeight, setSelectedRowKeys, diff --git a/src/components/Table/src/hooks/useDataSource.ts b/src/components/Table/src/hooks/useDataSource.ts index 03c6a894935..60abceb0c86 100644 --- a/src/components/Table/src/hooks/useDataSource.ts +++ b/src/components/Table/src/hooks/useDataSource.ts @@ -160,6 +160,31 @@ export function useDataSource( } } + function deleteTableDataRecord(record: Recordable | Recordable[]): Recordable | undefined { + if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; + const records = !Array.isArray(record) ? [record] : record; + const recordIndex = records + .map((item) => dataSourceRef.value.findIndex((s) => s.key === item.key)) // 取序号 + .filter((item) => item !== undefined) + .sort((a, b) => b - a); // 从大到小排序 + for (const index of recordIndex) { + unref(dataSourceRef).splice(index, 1); + unref(propsRef).dataSource?.splice(index, 1); + } + setPagination({ + total: unref(propsRef).dataSource?.length, + }); + return unref(propsRef).dataSource; + } + + function insertTableDataRecord(record: Recordable, index: number): Recordable | undefined { + if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; + index = index ?? dataSourceRef.value?.length; + unref(dataSourceRef).splice(index, 0, record); + unref(propsRef).dataSource?.splice(index, 0, record); + return unref(propsRef).dataSource; + } + function findTableDataRecord(rowKey: string | number) { if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; @@ -314,6 +339,8 @@ export function useDataSource( reload, updateTableData, updateTableDataRecord, + deleteTableDataRecord, + insertTableDataRecord, findTableDataRecord, handleTableChange, }; diff --git a/src/components/Table/src/hooks/useTable.ts b/src/components/Table/src/hooks/useTable.ts index 2a3377f7583..26139fe6f99 100644 --- a/src/components/Table/src/hooks/useTable.ts +++ b/src/components/Table/src/hooks/useTable.ts @@ -122,6 +122,12 @@ export function useTable(tableProps?: Props): [ updateTableData: (index: number, key: string, value: any) => { return getTableInstance().updateTableData(index, key, value); }, + deleteTableDataRecord: (record: Recordable | Recordable[]) => { + return getTableInstance().deleteTableDataRecord(record); + }, + insertTableDataRecord: (record: Recordable | Recordable[], index?: number) => { + return getTableInstance().insertTableDataRecord(record, index); + }, updateTableDataRecord: (rowKey: string | number, record: Recordable) => { return getTableInstance().updateTableDataRecord(rowKey, record); }, diff --git a/src/components/Table/src/types/table.ts b/src/components/Table/src/types/table.ts index 2cf32ec4f35..7660b0fc271 100644 --- a/src/components/Table/src/types/table.ts +++ b/src/components/Table/src/types/table.ts @@ -95,6 +95,8 @@ export interface TableActionType { setPagination: (info: Partial) => void; setTableData: (values: T[]) => void; updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void; + deleteTableDataRecord: (record: Recordable | Recordable[]) => Recordable | void; + insertTableDataRecord: (record: Recordable, index?: number) => Recordable | void; findTableDataRecord: (rowKey: string | number) => Recordable | void; getColumns: (opt?: GetColumnsParams) => BasicColumn[]; setColumns: (columns: BasicColumn[] | string[]) => void;