Merge pull request #406 from paynezhuang/feat/table-total-items

feat(projects): add table showTotal options
This commit is contained in:
Soybean 2024-04-25 23:31:59 +08:00 committed by GitHub
commit de9567420a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 28 additions and 4 deletions

View File

@ -57,6 +57,12 @@ export type TableConfig<A extends ApiFn, T, C> = {
* @default true * @default true
*/ */
immediate?: boolean; immediate?: boolean;
/**
* whether to display the total items count
*
* @default false
*/
showTotal?: boolean;
}; };
export default function useHookTable<A extends ApiFn, T, C>(config: TableConfig<A, T, C>) { export default function useHookTable<A extends ApiFn, T, C>(config: TableConfig<A, T, C>) {

View File

@ -13,7 +13,9 @@ export function useTable<A extends NaiveUI.TableApiFn>(config: NaiveUI.NaiveTabl
const scope = effectScope(); const scope = effectScope();
const appStore = useAppStore(); const appStore = useAppStore();
const { apiFn, apiParams, immediate } = config; const isMobile = computed(() => appStore.isMobile);
const { apiFn, apiParams, immediate, showTotal } = config;
const SELECTION_KEY = '__selection__'; const SELECTION_KEY = '__selection__';
@ -124,14 +126,20 @@ export function useTable<A extends NaiveUI.TableApiFn>(config: NaiveUI.NaiveTabl
}); });
getData(); getData();
},
...(showTotal
? {
prefix: page => $t('datatable.itemCount', { total: page.itemCount })
} }
: {})
}); });
// this is for mobile, if the system does not support mobile, you can use `pagination` directly // this is for mobile, if the system does not support mobile, you can use `pagination` directly
const mobilePagination = computed(() => { const mobilePagination = computed(() => {
const p: PaginationProps = { const p: PaginationProps = {
...pagination, ...pagination,
pageSlot: appStore.isMobile ? 3 : 9 pageSlot: isMobile.value ? 3 : 9,
prefix: !isMobile.value && showTotal ? pagination.prefix : undefined
}; };
return p; return p;

View File

@ -446,6 +446,9 @@ const local: App.I18n.Schema = {
expand: 'Expand Menu', expand: 'Expand Menu',
pin: 'Pin', pin: 'Pin',
unpin: 'Unpin' unpin: 'Unpin'
},
datatable: {
itemCount: 'Total {total} items'
} }
}; };

View File

@ -446,6 +446,9 @@ const local: App.I18n.Schema = {
expand: '展开菜单', expand: '展开菜单',
pin: '固定', pin: '固定',
unpin: '取消固定' unpin: '取消固定'
},
datatable: {
itemCount: '共 {total} 条'
} }
}; };

View File

@ -605,6 +605,9 @@ declare namespace App {
pin: string; pin: string;
unpin: string; unpin: string;
}; };
datatable: {
itemCount: string;
};
}; };
type GetI18nKey<T extends Record<string, unknown>, K extends keyof T = keyof T> = K extends string type GetI18nKey<T extends Record<string, unknown>, K extends keyof T = keyof T> = K extends string

View File

@ -42,6 +42,6 @@ declare namespace NaiveUI {
type NaiveTableConfig<A extends TableApiFn> = Pick< type NaiveTableConfig<A extends TableApiFn> = Pick<
import('@sa/hooks').TableConfig<A, GetTableData<A>, TableColumn<TableDataWithIndex<GetTableData<A>>>>, import('@sa/hooks').TableConfig<A, GetTableData<A>, TableColumn<TableDataWithIndex<GetTableData<A>>>>,
'apiFn' | 'apiParams' | 'columns' | 'immediate' 'apiFn' | 'apiParams' | 'columns' | 'immediate' | 'showTotal'
>; >;
} }

View File

@ -12,6 +12,7 @@ const appStore = useAppStore();
const { columns, columnChecks, data, getData, loading, mobilePagination, searchParams, resetSearchParams } = useTable({ const { columns, columnChecks, data, getData, loading, mobilePagination, searchParams, resetSearchParams } = useTable({
apiFn: fetchGetUserList, apiFn: fetchGetUserList,
showTotal: true,
apiParams: { apiParams: {
current: 1, current: 1,
size: 10, size: 10,