| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import type { PaginationProps } from '../types/pagination';
- import type { BasicTableProps } from '../types/table';
- import { computed, unref, ref, ComputedRef, watch } from 'vue';
- import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue';
- import { isBoolean } from '/@/utils/is';
- import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '../const';
- import { useI18n } from '/@/hooks/web/useI18n';
- import { Select } from 'ant-design-vue';
- interface ItemRender {
- page: number;
- type: 'page' | 'prev' | 'next';
- originalElement: any;
- }
- function itemRender({ page, type, originalElement }: ItemRender) {
- if (type === 'prev') {
- return page === 0 ? null : <LeftOutlined />;
- } else if (type === 'next') {
- return page === 1 ? null : <RightOutlined />;
- }
- return originalElement;
- }
- export function usePagination(refProps: ComputedRef<BasicTableProps>) {
- const { t } = useI18n();
- const configRef = ref<PaginationProps>({});
- const show = ref(true);
- watch(
- () => unref(refProps).pagination,
- (pagination) => {
- if (!isBoolean(pagination) && pagination) {
- configRef.value = {
- ...unref(configRef),
- ...(pagination ?? {}),
- selectComponentClass: () => (
- <Select
- defaultValue={configRef.value.pageSize}
- options={[
- { label: '5条每页', value: 5 },
- { label: '10条每页', value: 10 },
- { label: '30条每页', value: 30 },
- { label: '50条每页', value: 50 },
- { label: '100条每页', value: 100 },
- ]}
- style="width: 100px; margin-right: 10px;"
- onChange={updataPageSize}
- ></Select>
- ),
- };
- }
- }
- );
- const getPaginationInfo = computed((): PaginationProps | boolean => {
- const { pagination } = unref(refProps);
- if (!unref(show) || (isBoolean(pagination) && !pagination)) {
- return false;
- }
- return {
- current: 1,
- pageSize: PAGE_SIZE,
- size: 'small',
- defaultPageSize: PAGE_SIZE,
- showTotal: (total) => t('component.table.total', { total }),
- showSizeChanger: true,
- pageSizeOptions: PAGE_SIZE_OPTIONS,
- itemRender: itemRender,
- showQuickJumper: true,
- // ...(isBoolean(pagination) ? {} : pagination),
- ...unref(configRef),
- };
- });
- function setPagination(info: Partial<PaginationProps>) {
- const paginationInfo = unref(getPaginationInfo);
- configRef.value = {
- ...(!isBoolean(paginationInfo) ? paginationInfo : {}),
- ...info,
- selectComponentClass: () => (
- <Select
- defaultValue={configRef.value.pageSize}
- options={[
- { label: '5条每页', value: 5 },
- { label: '10条每页', value: 10 },
- { label: '30条每页', value: 30 },
- { label: '50条每页', value: 50 },
- { label: '100条每页', value: 100 },
- ]}
- style="width: 100px; margin-right: 10px;"
- onChange={updataPageSize}
- ></Select>
- ),
- };
- }
- function updataPageSize(size) {
- configRef.value.pageSize = size;
- }
- function getPagination() {
- return unref(getPaginationInfo);
- }
- function getShowPagination() {
- return unref(show);
- }
- async function setShowPagination(flag: boolean) {
- show.value = flag;
- }
- return { getPagination, getPaginationInfo, setShowPagination, getShowPagination, setPagination };
- }
|