From be6684165372dfd7b43593131fb9275413d4b43d Mon Sep 17 00:00:00 2001 From: Azir <2075125282@qq.com> Date: Tue, 13 Aug 2024 16:45:34 +0800 Subject: [PATCH 1/2] feat(hooks): useTable supports passing in the default transformer close#595 --- src/hooks/common/table.ts | 44 ++++++++++++++++++++------------------- src/typings/naive-ui.d.ts | 7 +++++++ 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/hooks/common/table.ts b/src/hooks/common/table.ts index 6619f8b3e..3741a60fd 100644 --- a/src/hooks/common/table.ts +++ b/src/hooks/common/table.ts @@ -16,12 +16,33 @@ export function useTable(config: NaiveUI.NaiveTabl const isMobile = computed(() => appStore.isMobile); - const { apiFn, apiParams, immediate, showTotal } = config; + const { apiFn, transformer, apiParams, immediate, showTotal } = config; const SELECTION_KEY = '__selection__'; const EXPAND_KEY = '__expand__'; + const defaultTransformer = (res: Awaited>) => { + const { records = [], current = 1, size = 10, total = 0 } = res.data || {}; + + // Ensure that the size is greater than 0, If it is less than 0, it will cause paging calculation errors. + const pageSize = size <= 0 ? 10 : size; + + const recordsWithIndex = records.map((item, index) => { + return { + ...item, + index: (current - 1) * pageSize + index + 1 + }; + }); + + return { + data: recordsWithIndex, + pageNum: current, + pageSize, + total + }; + }; + const { loading, empty, @@ -37,26 +58,7 @@ export function useTable(config: NaiveUI.NaiveTabl apiFn, apiParams, columns: config.columns, - transformer: res => { - const { records = [], current = 1, size = 10, total = 0 } = res.data || {}; - - // Ensure that the size is greater than 0, If it is less than 0, it will cause paging calculation errors. - const pageSize = size <= 0 ? 10 : size; - - const recordsWithIndex = records.map((item, index) => { - return { - ...item, - index: (current - 1) * pageSize + index + 1 - }; - }); - - return { - data: recordsWithIndex, - pageNum: current, - pageSize, - total - }; - }, + transformer: transformer || defaultTransformer, getColumnChecks: cols => { const checks: NaiveUI.TableColumnCheck[] = []; diff --git a/src/typings/naive-ui.d.ts b/src/typings/naive-ui.d.ts index cde80bab3..b13d8d1f7 100644 --- a/src/typings/naive-ui.d.ts +++ b/src/typings/naive-ui.d.ts @@ -50,5 +50,12 @@ declare namespace NaiveUI { * @default false */ showTotal?: boolean; + /** + * the custom transformer function + * + * @param res the response data + * @returns the transformed data + */ + transformer?: (res: Awaited>) => { data: any[]; pageNum: number; pageSize: number; total: number }; }; } From 347deb3cec8c23eb58531b0e25adfa6686a82099 Mon Sep 17 00:00:00 2001 From: Azir <2075125282@qq.com> Date: Tue, 13 Aug 2024 16:50:13 +0800 Subject: [PATCH 2/2] fix(types): fix the type of TableApiFn --- src/typings/api.d.ts | 3 +++ src/typings/naive-ui.d.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/typings/api.d.ts b/src/typings/api.d.ts index bb3d8757e..9e88ca688 100644 --- a/src/typings/api.d.ts +++ b/src/typings/api.d.ts @@ -15,6 +15,9 @@ declare namespace Api { total: number; } + /** common search params of table */ + type CommonSearchParams = Pick; + /** common params of paginating query list data */ interface PaginatingQueryRecord extends PaginatingCommonParams { records: T[]; diff --git a/src/typings/naive-ui.d.ts b/src/typings/naive-ui.d.ts index b13d8d1f7..1765fe461 100644 --- a/src/typings/naive-ui.d.ts +++ b/src/typings/naive-ui.d.ts @@ -26,7 +26,7 @@ declare namespace NaiveUI { type TableColumn = TableColumnWithKey | DataTableSelectionColumn | DataTableExpandColumn; - type TableApiFn = ( + type TableApiFn = ( params: R ) => Promise>>;