import {Pagination} from "../../Pagination"; import {computed, reactive} from "@peryl/react-compose"; import {iAutoTablePageData} from "../utils/AutoTable.utils"; import $notice from "../../$notice"; import {deepcopy} from "@peryl/utils/deepcopy"; import '../utils/TableOption.space'; import i18n from "../../i18n"; import {AutoModule} from "../utils/AutoModule"; declare module '../utils/TableOption.space' { namespace TableOptionSpace { interface iTableOption { pagination: ReturnType; } } } export const useTableOptionPagination = AutoModule.createRegistration(( { state: tableState, hooks, methods: tableMethods, config, keyboard, } ) => { const methods = { createInitialData: (): iAutoTablePageData => { return { page: 1, size: config.pageSize || config.showRows, hasNext: true, total: null as null | number, }; }, changeSize: async (size: number) => {await tableMethods.pageMethods.reload({ size });}, jump: async (page: number) => { if (page == state.page) { return; } if (page < 0) { page = 1; return tableMethods.pageMethods.load({ page }); } if (page <= totalPage.value) { return tableMethods.pageMethods.load({ page }); } else { if (state.total != null) { if (page > totalPage.value) { page = totalPage.value; } return tableMethods.pageMethods.load({ page }); } else { const total = await tableMethods.pageMethods.queryCount(); if (!total) { const msg = i18n.$it('table.queryTotalFailed').d('查询总数失败'); $notice.error(msg); throw new Error(msg); } if (page > totalPage.value) {page = totalPage.value;} return tableMethods.pageMethods.load({ page }); } } }, }; const state = reactive({ ...methods.createInitialData(), pageSizeOptions: deepcopy(config.pageSizeOptions), }); if (!!config.showRows && state.pageSizeOptions.indexOf(config.showRows) === -1) { const index = state.pageSizeOptions.findIndex(i => i > config.showRows); state.pageSizeOptions.splice(index, 0, config.showRows); } const handler = { onSizeChange: async (size: number) => {await methods.changeSize(size);}, onJump: async (page: number) => {await methods.jump(page);}, }; const total = computed(() => { if (state.total != null) { return state.total; } if (state.hasNext) { return (state.page) * state.size + 1; } else { return (state.page - 1) * state.size + tableState.list.length; } }); const totalPage = computed(() => Math.ceil(total.value / state.size)); hooks.onGetPageState.use(() => {return state;}); hooks.onTableRender.use({ processor: ({ list, render }) => {list.push({ key: 'pagination', seq: 6, render });}, render: () => (
{i18n.$it('base.total').d('总共')} {state.total == null ? {total.value}+ : total.value} {i18n.$it('base.data').d('条数据')}
), }); hooks.onBeforeReload.use(() => { state.hasNext = true; state.total = null; }); hooks.onPageDataChange.use((pageData) => { if (!pageData) { pageData = methods.createInitialData(); } state.page = pageData.page; state.size = pageData.size; if (state.total != null) { /*已经得到总数*/ return; } if (!pageData.hasNext) { /*没有更多数据了*/ state.hasNext = false; state.total = (state.page - 1) * state.size + tableState.list.length; } }); hooks.onQueryCount.use(({ total }) => { state.total = total; state.hasNext = false; }); keyboard.on('left', () => {methods.jump(state.page - 1);}); keyboard.on('right', () => {methods.jump(state.page + 1);}); keyboard.on('up', async () => { try { const node = await tableMethods.utils.getCurrentNode(); const nextRowKey = tableState.list[node.state.index - 1]?.[config.keyField]; if (node.state.index - 1 === -1 && state.page !== 1) { return methods.jump(state.page - 1); } if (!!nextRowKey) { await tableMethods.utils.selectCurrent(nextRowKey); } } catch (e) {/*do nothing*/} }); keyboard.on('down', async () => { try { const node = await tableMethods.utils.getCurrentNode(); if (node.state.index + 1 === tableState.list.length && state.hasNext) { return methods.jump(state.page + 1); } const nextRowKey = tableState.list[node.state.index + 1]?.[config.keyField]; if (!!nextRowKey) { await tableMethods.utils.selectCurrent(nextRowKey); } } catch (e) {/*do nothing*/} }); return { state, methods, }; }); export type iTableOptionPagination = ReturnType