mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-09-29 14:46:41 +08:00
feat(hooks): useTable supports passing in the default transformer close#595
This commit is contained in:
parent
5094f0eea8
commit
be66841653
@ -16,12 +16,33 @@ export function useTable<A extends NaiveUI.TableApiFn>(config: NaiveUI.NaiveTabl
|
|||||||
|
|
||||||
const isMobile = computed(() => appStore.isMobile);
|
const isMobile = computed(() => appStore.isMobile);
|
||||||
|
|
||||||
const { apiFn, apiParams, immediate, showTotal } = config;
|
const { apiFn, transformer, apiParams, immediate, showTotal } = config;
|
||||||
|
|
||||||
const SELECTION_KEY = '__selection__';
|
const SELECTION_KEY = '__selection__';
|
||||||
|
|
||||||
const EXPAND_KEY = '__expand__';
|
const EXPAND_KEY = '__expand__';
|
||||||
|
|
||||||
|
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.
|
||||||
|
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 {
|
const {
|
||||||
loading,
|
loading,
|
||||||
empty,
|
empty,
|
||||||
@ -37,26 +58,7 @@ export function useTable<A extends NaiveUI.TableApiFn>(config: NaiveUI.NaiveTabl
|
|||||||
apiFn,
|
apiFn,
|
||||||
apiParams,
|
apiParams,
|
||||||
columns: config.columns,
|
columns: config.columns,
|
||||||
transformer: res => {
|
transformer: transformer || defaultTransformer,
|
||||||
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
|
|
||||||
};
|
|
||||||
},
|
|
||||||
getColumnChecks: cols => {
|
getColumnChecks: cols => {
|
||||||
const checks: NaiveUI.TableColumnCheck[] = [];
|
const checks: NaiveUI.TableColumnCheck[] = [];
|
||||||
|
|
||||||
|
7
src/typings/naive-ui.d.ts
vendored
7
src/typings/naive-ui.d.ts
vendored
@ -50,5 +50,12 @@ declare namespace NaiveUI {
|
|||||||
* @default false
|
* @default false
|
||||||
*/
|
*/
|
||||||
showTotal?: boolean;
|
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 };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user