Skip to content

Commit

Permalink
Merge pull request #406 from paynezhuang/feat/table-total-items
Browse files Browse the repository at this point in the history
feat(projects): add table showTotal options
  • Loading branch information
honghuangdc committed Apr 25, 2024
2 parents ec5b1c3 + 3e61eab commit de95674
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/hooks/src/use-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export type TableConfig<A extends ApiFn, T, C> = {
* @default true
*/
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>) {
Expand Down
14 changes: 11 additions & 3 deletions src/hooks/common/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export function useTable<A extends NaiveUI.TableApiFn>(config: NaiveUI.NaiveTabl
const scope = effectScope();
const appStore = useAppStore();

const { apiFn, apiParams, immediate } = config;
const isMobile = computed(() => appStore.isMobile);

const { apiFn, apiParams, immediate, showTotal } = config;

const SELECTION_KEY = '__selection__';

Expand Down Expand Up @@ -124,14 +126,20 @@ export function useTable<A extends NaiveUI.TableApiFn>(config: NaiveUI.NaiveTabl
});

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
const mobilePagination = computed(() => {
const p: PaginationProps = {
...pagination,
pageSlot: appStore.isMobile ? 3 : 9
pageSlot: isMobile.value ? 3 : 9,
prefix: !isMobile.value && showTotal ? pagination.prefix : undefined
};

return p;
Expand Down
3 changes: 3 additions & 0 deletions src/locales/langs/en-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ const local: App.I18n.Schema = {
expand: 'Expand Menu',
pin: 'Pin',
unpin: 'Unpin'
},
datatable: {
itemCount: 'Total {total} items'
}
};

Expand Down
3 changes: 3 additions & 0 deletions src/locales/langs/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ const local: App.I18n.Schema = {
expand: '展开菜单',
pin: '固定',
unpin: '取消固定'
},
datatable: {
itemCount: '共 {total} 条'
}
};

Expand Down
3 changes: 3 additions & 0 deletions src/typings/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,9 @@ declare namespace App {
pin: string;
unpin: string;
};
datatable: {
itemCount: string;
};
};

type GetI18nKey<T extends Record<string, unknown>, K extends keyof T = keyof T> = K extends string
Expand Down
2 changes: 1 addition & 1 deletion src/typings/naive-ui.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ declare namespace NaiveUI {

type NaiveTableConfig<A extends TableApiFn> = Pick<
import('@sa/hooks').TableConfig<A, GetTableData<A>, TableColumn<TableDataWithIndex<GetTableData<A>>>>,
'apiFn' | 'apiParams' | 'columns' | 'immediate'
'apiFn' | 'apiParams' | 'columns' | 'immediate' | 'showTotal'
>;
}
1 change: 1 addition & 0 deletions src/views/manage/user/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const appStore = useAppStore();
const { columns, columnChecks, data, getData, loading, mobilePagination, searchParams, resetSearchParams } = useTable({
apiFn: fetchGetUserList,
showTotal: true,
apiParams: {
current: 1,
size: 10,
Expand Down

0 comments on commit de95674

Please sign in to comment.