feat(hooks): useTable supports passing in the default transformer close#595

This commit is contained in:
Azir 2024-08-13 16:45:34 +08:00
parent 5094f0eea8
commit be66841653
2 changed files with 30 additions and 21 deletions

View File

@ -16,28 +16,13 @@ export function useTable<A extends NaiveUI.TableApiFn>(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 {
loading,
empty,
data,
columns,
columnChecks,
reloadColumns,
getData,
searchParams,
updateSearchParams,
resetSearchParams
} = useHookTable<A, GetTableData<A>, TableColumn<NaiveUI.TableDataWithIndex<GetTableData<A>>>>({
apiFn,
apiParams,
columns: config.columns,
transformer: res => {
const defaultTransformer = (res: Awaited<ReturnType<A>>) => {
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.
@ -56,7 +41,24 @@ export function useTable<A extends NaiveUI.TableApiFn>(config: NaiveUI.NaiveTabl
pageSize,
total
};
},
};
const {
loading,
empty,
data,
columns,
columnChecks,
reloadColumns,
getData,
searchParams,
updateSearchParams,
resetSearchParams
} = useHookTable<A, GetTableData<A>, TableColumn<NaiveUI.TableDataWithIndex<GetTableData<A>>>>({
apiFn,
apiParams,
columns: config.columns,
transformer: transformer || defaultTransformer,
getColumnChecks: cols => {
const checks: NaiveUI.TableColumnCheck[] = [];

View File

@ -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<ReturnType<A>>) => { data: any[]; pageNum: number; pageSize: number; total: number };
};
}