封装分页pagination组件

/**
 * @file pagination
 */
import {Button, Input, Pagination, PaginationProps, Select} from 'antd';
import React, {ChangeEvent, FC, useCallback, useEffect, useState} from 'react';

import './index.less';

function countMaxPage(total: number, pageSize: number) {
    return Math.ceil(total / pageSize);
}

const BizPagination: FC<PaginationProps> = (props: PaginationProps) => {
    const {pageSize, pageSizeOptions, current, showSizeChanger, onChange, total, ...others} = props;
    const [pageSizeInner, setPageSizeInner] = useState(10);
    const [currentInner, setCurrentInner] = useState(1);
    const [jumperPageNo, setJumperPageNo] = useState('');

    useEffect(() => {
        if (typeof pageSize === 'number') {
            setPageSizeInner(pageSize);
        }
    }, [pageSize]
);

    useEffect(() => {
        if (typeof current === 'number') {
            setCurrentInner(current);
        }
    }, [current]
);

    const handleChange = useCallback(
        (page: number, pageSize: number) => {
            if (typeof onChange === 'function') {
                onChange(page, pageSize);
            }
        },
        [onChange]
    );

    const handlePagerOptionsChange = useCallback(
        (value) => {
            const val = Number(value);
            setPageSizeInner(val);
            const maxPage = countMaxPage(Number(total), val);
            let current = currentInner;
            if (currentInner > maxPage) {
                current = maxPage;
                setCurrentInner(current);
            }
            handleChange(current, val);
        },
        [handleChange, currentInner, total]
    );

    const handlePagerChange = useCallback(
        (page) => {
            setCurrentInner(page);
            handleChange(page, pageSizeInner);
        },
        [handleChange, pageSizeInner]
    );

    const inputJumperPageNo = useCallback((e: ChangeEvent<HTMLInputElement>) => {
        setJumperPageNo(e.target.value);
    }, []
);

    const jumpToPage = useCallback(() => {
        if (jumperPageNo && Number(jumperPageNo) > 0) {
            let pageNo = Number(jumperPageNo);
            const maxPage = countMaxPage(Number(total), pageSizeInner);
            if (pageNo > maxPage) {
                pageNo = maxPage;
            }
            handleChange(pageNo, pageSizeInner);
        }
        setJumperPageNo('');
    }, [jumperPageNo, handleChange, pageSizeInner, total]
);

    if (others.hideOnSinglePage && (!total || total <= pageSizeInner)) {
        return null;
    }

    return (
        <div className="biz-pagination">
            {showSizeChanger && (
                <div className="biz-pagination-options">
                    <span>每页显示</span>
                    <Select className="biz-pagination-select" value={pageSizeInner} onChange={handlePagerOptionsChange}>
                        {pageSizeOptions?.map((opt) => (
                            <Select.Option value={opt} key={opt}>
                                {opt}
                            </Select.Option>
                        ))}
                    </Select>
                </div>
            )}
            <Pagination
                {...others}
                total={total}
                pageSize={pageSizeInner}
                current={currentInner}
                showQuickJumper={false}
                showSizeChanger={false}
                onChange={handlePagerChange}
            />
            {showSizeChanger && (
                <div className="biz-pagination-jumper">
                    <span className="biz-pagination-jumper-text">跳转至</span>
                    <Input.Group compact>
                        <Input
                            className="biz-pagination-jumper-input"
                            value={jumperPageNo}
                            onChange={inputJumperPageNo}
                        />
                        <Button className="biz-pagination-jumper-btn" onClick={jumpToPage}>
                            GO
                        </Button>
                    </Input.Group>
                </div>
            )}
        </div>
    );
};

BizPagination.defaultProps = {
    current: 1,
    pageSize: 10,
    total: 1,
    pageSizeOptions: ['10', '20', '50']
};

export default BizPagination;

index.css

.biz-pagination {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin: 16px 0;

    &-options {
        color: rgba(8, 14, 26, .5);
    }

    &-select {
        width: 80px;
        margin: 0 8px;
        border-color: rgba(8, 14, 26, .2);
    }

    &-jumper {
        display: flex;
        align-items: center;
        justify-content: center;

        &-text {
            flex: 0 0 auto;
            margin: 0 8px;
            color: rgba(8, 14, 26, .5);
        }

        & &-input {
            width: 40px;
            height: 32px;
            border-color: rgba(8, 14, 26, .2);
        }

        &-btn {
            width: 40px;
            padding: 0;
            border-color: rgba(8, 14, 26, .2);
            color: rgba(8, 14, 26, .95);
        }
    }

    .ant-pagination-item {
        margin: 0;
    }

    .ant-pagination-item a {
        color: rgba(8, 14, 26, .95);
    }

    .ant-pagination-item,
    .ant-pagination-prev,
    .ant-pagination-next {
        border-width: 0;

        & .ant-pagination-item-link {
            border-width: 0;
        }
    }

    .ant-pagination-item-active {
        border-width: 1px;

        & a {
            color: @primary-color;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Vue3中封装分页组件的方法与Vue2有所不同。以下是一个基本的封装分页组件的方法: 1. 首先,在你的项目中创建一个名为`Pagination.vue`的组件文件。 2. 在`Pagination.vue`组件中,你可以使用`<template>`标签来定义组件的结构。可以使用`<div>`标签来包裹分页组件的内容,比如页码和按钮。 3. 在`<script>`标签中,你需要导入`vue`并声明组件。你可以使用`ref`来追踪当前页码,并且使用`computed`属性来计算总页数。 4. 在组件内部,你可以创建一个`methods`对象,并在其中定义一些方法来处理页码的变化。比如,你可以创建`goToPage`方法来跳转到指定的页码。 5. 最后,在`<style>`标签中,你可以定义组件的样式,如页码的颜色和按钮的样式。 在你的项目中使用这个封装的分页组件的方法如下: 1. 在需要使用分页功能的组件中,使用`import`关键字导入刚刚封装的`Pagination`组件。 2. 在`components`属性中注册`Pagination`组件。 3. 在`<template>`标签中使用自定义的分页组件。可以使用`v-model`指令来双向绑定当前页码。 通过上述步骤,你就可以在Vue3中封装一个分页组件并在项目中使用了。这个组件可以提供分页功能,让用户可以方便地切换页码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [基于Vue如何封装分页组件](https://download.csdn.net/download/weixin_38550605/12789728)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Vue3 element-ui实现Pagination分页组件--封装分页](https://blog.csdn.net/coinisi_li/article/details/128952886)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒枫落林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值