feat: 优化后台列表高度与分页容量
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { computed, h, reactive, ref, watch } from 'vue';
|
||||
import { NButton, NInputNumber } from 'naive-ui';
|
||||
import type { PaginationProps } from 'naive-ui';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
|
||||
export const TABLE_PAGE_SIZE_ALL = Number.MAX_SAFE_INTEGER;
|
||||
|
||||
export function useTablePage(load: () => void | Promise<void>, initialPageSize = 20) {
|
||||
const { t, locale } = useI18n();
|
||||
const customPageSize = ref<number | null>(null);
|
||||
|
||||
function pageSizeOptions() {
|
||||
return [
|
||||
{ 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 },
|
||||
];
|
||||
}
|
||||
|
||||
const pagination = reactive<PaginationProps>({
|
||||
page: 1,
|
||||
pageSize: initialPageSize,
|
||||
itemCount: 0,
|
||||
showSizePicker: true,
|
||||
showQuickJumper: true,
|
||||
pageSizes: pageSizeOptions(),
|
||||
displayOrder: ['pages', 'size-picker', 'quick-jumper'],
|
||||
suffix: () =>
|
||||
h('span', { class: 'table-pagination-extra' }, [
|
||||
h(NInputNumber, {
|
||||
value: customPageSize.value,
|
||||
min: 1,
|
||||
precision: 0,
|
||||
size: 'small',
|
||||
placeholder: t('common.pagination.customPageSize'),
|
||||
style: 'width: 116px',
|
||||
showButton: false,
|
||||
onUpdateValue: (value: number | null) => {
|
||||
customPageSize.value = value;
|
||||
},
|
||||
onKeyup: (event: KeyboardEvent) => {
|
||||
if (event.key === 'Enter') applyCustomPageSize(customPageSize.value);
|
||||
},
|
||||
}),
|
||||
h(
|
||||
NButton,
|
||||
{
|
||||
size: 'small',
|
||||
disabled: !customPageSize.value || customPageSize.value < 1,
|
||||
onClick: () => applyCustomPageSize(customPageSize.value),
|
||||
},
|
||||
{ default: () => t('common.pagination.apply') },
|
||||
),
|
||||
]),
|
||||
onUpdatePage: (page: number) => {
|
||||
pagination.page = page;
|
||||
void load();
|
||||
},
|
||||
onUpdatePageSize: (pageSize: number) => {
|
||||
pagination.page = 1;
|
||||
pagination.pageSize = pageSize;
|
||||
void load();
|
||||
},
|
||||
});
|
||||
|
||||
const page = computed(() => pagination.page || 1);
|
||||
const pageSize = computed(() => ((pagination.pageSize ?? initialPageSize) === TABLE_PAGE_SIZE_ALL ? 0 : (pagination.pageSize ?? initialPageSize)));
|
||||
|
||||
function resetPage() {
|
||||
pagination.page = 1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function applyCustomPageSize(value: number | null) {
|
||||
if (!value || value < 1) return;
|
||||
pagination.page = 1;
|
||||
pagination.pageSize = Math.floor(value);
|
||||
customPageSize.value = null;
|
||||
void load();
|
||||
}
|
||||
|
||||
watch(locale, () => {
|
||||
pagination.pageSizes = pageSizeOptions();
|
||||
});
|
||||
|
||||
return { pagination, page, pageSize, resetPage, setPageResult, applyCustomPageSize };
|
||||
}
|
||||
Reference in New Issue
Block a user