fix: 修复后台列表分页交互
This commit is contained in:
@@ -4,19 +4,30 @@ import type { PaginationProps } from 'naive-ui';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
|
||||
export const TABLE_PAGE_SIZE_ALL = Number.MAX_SAFE_INTEGER;
|
||||
const TABLE_PAGE_SIZE_CUSTOM = Number.MAX_SAFE_INTEGER - 1;
|
||||
|
||||
export function useTablePage(load: () => void | Promise<void>, initialPageSize = 20) {
|
||||
const { t, locale } = useI18n();
|
||||
const showCustomPageSize = ref(false);
|
||||
const customPageSize = ref<number | null>(null);
|
||||
const appliedCustomPageSize = ref<number | null>(null);
|
||||
|
||||
function pageSizeOptions() {
|
||||
return [
|
||||
const options = [
|
||||
{ label: t('common.pagination.perPage', { n: 20 }), value: 20 },
|
||||
{ label: t('common.pagination.perPage', { n: 50 }), value: 50 },
|
||||
{ label: t('common.pagination.perPage', { n: 100 }), value: 100 },
|
||||
{ label: t('common.pagination.perPage', { n: 200 }), value: 200 },
|
||||
{ label: t('common.pagination.unlimited'), value: TABLE_PAGE_SIZE_ALL },
|
||||
{ label: t('common.pagination.customPageSize'), value: TABLE_PAGE_SIZE_CUSTOM },
|
||||
];
|
||||
if (appliedCustomPageSize.value && !options.some((item) => item.value === appliedCustomPageSize.value)) {
|
||||
options.splice(options.length - 1, 0, {
|
||||
label: t('common.pagination.perPage', { n: appliedCustomPageSize.value }),
|
||||
value: appliedCustomPageSize.value,
|
||||
});
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
const pagination = reactive<PaginationProps>({
|
||||
@@ -27,8 +38,9 @@ export function useTablePage(load: () => void | Promise<void>, initialPageSize =
|
||||
showQuickJumper: true,
|
||||
pageSizes: pageSizeOptions(),
|
||||
displayOrder: ['pages', 'size-picker', 'quick-jumper'],
|
||||
suffix: () =>
|
||||
h('span', { class: 'table-pagination-extra' }, [
|
||||
suffix: () => {
|
||||
if (!showCustomPageSize.value) return null;
|
||||
return h('span', { class: 'table-pagination-extra' }, [
|
||||
h(NInputNumber, {
|
||||
value: customPageSize.value,
|
||||
min: 1,
|
||||
@@ -53,12 +65,20 @@ export function useTablePage(load: () => void | Promise<void>, initialPageSize =
|
||||
},
|
||||
{ default: () => t('common.pagination.apply') },
|
||||
),
|
||||
]),
|
||||
]);
|
||||
},
|
||||
onUpdatePage: (page: number) => {
|
||||
pagination.page = page;
|
||||
void load();
|
||||
},
|
||||
onUpdatePageSize: (pageSize: number) => {
|
||||
if (pageSize === TABLE_PAGE_SIZE_CUSTOM) {
|
||||
showCustomPageSize.value = true;
|
||||
customPageSize.value = appliedCustomPageSize.value || 20;
|
||||
pagination.pageSize = appliedCustomPageSize.value || initialPageSize;
|
||||
return;
|
||||
}
|
||||
showCustomPageSize.value = false;
|
||||
pagination.page = 1;
|
||||
pagination.pageSize = pageSize;
|
||||
void load();
|
||||
@@ -75,13 +95,21 @@ export function useTablePage(load: () => void | Promise<void>, initialPageSize =
|
||||
function setPageResult(result: { total: number; page: number; pageSize: number }) {
|
||||
pagination.itemCount = result.total;
|
||||
pagination.page = result.page;
|
||||
pagination.pageSize = result.pageSize === 0 ? TABLE_PAGE_SIZE_ALL : result.pageSize;
|
||||
const nextPageSize = result.pageSize === 0 ? TABLE_PAGE_SIZE_ALL : result.pageSize;
|
||||
if (nextPageSize !== TABLE_PAGE_SIZE_ALL && ![20, 50, 100, 200].includes(nextPageSize)) {
|
||||
appliedCustomPageSize.value = nextPageSize;
|
||||
pagination.pageSizes = pageSizeOptions();
|
||||
}
|
||||
pagination.pageSize = nextPageSize;
|
||||
}
|
||||
|
||||
function applyCustomPageSize(value: number | null) {
|
||||
if (!value || value < 1) return;
|
||||
appliedCustomPageSize.value = Math.floor(value);
|
||||
pagination.pageSizes = pageSizeOptions();
|
||||
showCustomPageSize.value = false;
|
||||
pagination.page = 1;
|
||||
pagination.pageSize = Math.floor(value);
|
||||
pagination.pageSize = appliedCustomPageSize.value;
|
||||
customPageSize.value = null;
|
||||
void load();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user