vue Pagination 简单封装分页组件

7 篇文章 0 订阅
1 篇文章 0 订阅

示例

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Events

事件名称说明回调参数
current-changecurrent-change currentPage 改变时会触发 当前页当前页
prev-click用户点击上一页按钮改变当前页后触发当前页
next-click用户点击下一页按钮改变当前页后触发当前页

Attributes

参数说明类型可选值默认值
total总条数Number-0
currentPage当前页数 需添加.sync修饰符Number-1
pageSize每页显示条目个数Number-10
pageCount页码按钮的数量,当总页数超过该值时会折叠Number大于等于 5 且小于等于 21 的奇数7

页面代码

<template>
    <div class="Pagination">
        <!-- 上一页按钮 -->
        <button class="Pagination__item Pagination__button" :class="page === 1 ? 'disable' : ''" @click="prevClick">
            <span>{{leftIcon}}</span>
        </button>
        <!-- 显示页数 -->
        <div class="Pagination__ul">
            <div class="Pagination__item Pagination__li" :class="page === 1 ? 'action-current' : ''" @click="handlePage(1)">1</div>

            <div v-if="page - nums - 1 > 1 && totalPage > pageCount" class="Pagination__item Pagination__li" @click="prevEllipsisClick">...</div>

            <div v-for="item in pageList" :key="item">
                <div
                    class="Pagination__item Pagination__li"
                    :class="item === page ? 'action-current' : ''"
                    @click="handlePage(item)"
                >{{item}}</div>
            </div>

            <div v-if="page + nums + 1 < totalPage && totalPage > pageCount" class="Pagination__item Pagination__li" @click="nextEllipsisClick">...</div>

            <div class="Pagination__item Pagination__li" :class="page === totalPage ? 'action-current' : ''" @click="handlePage(totalPage)">{{totalPage}}</div>
        </div>
        <!-- 下一页按钮 -->
        <button class="Pagination__item Pagination__button" :class="page === totalPage ? 'disable' : ''" @click="nextClick">
            <span>{{rightIcon}}</span>
        </button>
        <!-- 自定义页数按钮 -->
        <div class="Pagination__input">
            <span>前往</span>
            <div class="p-input" :class="ifFocus ? 'focus' : ''">
                <input type="text" class="input" v-model="inputValue" @change="inputChange" @blur="ifFocus = false" @focus="ifFocus = true">
            </div>
            <span></span>
        </div>
    </div>
</template>

js代码

export default {
    name: 'pagination',
    data() {
        return {
            leftIcon: '<',
            rightIcon: '>',
            page: 1,
            size: 10,
            inputValue: '',
            ifFocus: false,
        }
    },
    computed: {
    	// 总分页数
        totalPage() {
            console.log(Math.ceil(this.total / this.pageSize), this.pageCount);
            return Math.ceil(this.total / this.pageSize);
        },
        nums() {
            return Math.floor((this.pageCount - 3) / 2)
        },
        // 分页数据列表
        pageList() {
            const listTotal = this.totalPage;
            let count = this.pageCount;
            let list = [];
            const num = this.nums
            // console.log(num);
            if (listTotal <= count) {
                for (let i = 0; i < listTotal - 2; i++) {
                    list.push(i + 2);
                }
            } else {
                if ((this.page - num - 1) <= 1) {
                    // 从2开始
                    for (let i = 0; i < count - num ; i++) {
                        list[i] = i + 2;
                    }
                } else if ((this.page + num + 1) >= listTotal) {
                    // 从listTotal减小
                    for (let i = 0; i < count - num; i++) {
                        list[i] = listTotal - (count -num - i);
                    }
                } else {
                    // 左边
                    for (let i = 0; i < num; i++) {
                        list[i] = this.page - (num - i);
                        // console.log(i + 1);
                    }
                    // 中间数
                    list[num] = this.page;
                    // console.log(num + 1);
                    // 右边数
                    for (let i = 0; i < num; i++) {
                        list[num + 1 + i] = this.page + i + 1;
                        // console.log(num + 2 + i);
                    }
                }
            }
            console.log(list);
            return list
        },
        pageCountFloor() {
            return Math.floor(this.pageCount / 2)
        }
    },
    watch: {
        currentPage(newValue) {
            if (newValue !== this.page) this.page = newValue;
        },
        page(newValue) {
            if (newValue !== this.inputValue) this.inputValue = newValue;
        },
    },
    created() {
        if (this.currentPage !== this.page) this.page = this.currentPage;
        if (this.page !== this.inputValue) this.inputValue = this.page;
    },
    methods: {
        // 点击页数
        handlePage(item) {
            if (item === this.page) return
            this.page = item;
            this.$emit('update:currentPage', item); 
            this.$emit('current-change', item); 
        },
        // 输入框改变
        inputChange(e) {
            const value = Number(e.target.value);
            if (value !== 'NaN' && this.$utils.strType(value) === 'Number') {
                if (value > 0 && value <= this.totalPage) {
                    this.handlePage(value)
                } else this.inputValue = this.page;
            } else this.inputValue = this.page;
        },
        // 上一页
        prevClick() {
            if (this.page === 1) return
            this.handlePage(this.page - 1)
        },
        // 下一页
        nextClick() {
            if (this.page === this.totalPage) return
            this.handlePage(this.page + 1)
        },
        nextEllipsisClick() {
            if (this.page === 1) this.handlePage(this.page + this.nums + 2);
            else this.handlePage(this.page + this.nums);
        },
        prevEllipsisClick() {
            console.log(this.nums);
            if (this.page === this.totalPage) this.handlePage(this.page - this.nums - 2);
            else this.handlePage(this.page - this.nums);
        },
    }
}

样式代码

<style lang="scss" scoped>
ul, li, ol {
    list-style: none;
    margin: 0;
    padding: 0;
}

.Pagination {
    display: flex;
    align-items: center;
    font-size: 16px;

    &__item {
        padding: 0 12px;
        cursor: default;

        &:hover {
            color: #409eff;
        }

        &.action-current {
            color: #409eff;
        }
    }

    &__button {
        color: #606266;
        background: transparent;
        border: none;
        outline: none;
    }

    &__ul {
        display: flex;
        align-items: center;
    }

    &__input {
        display: flex;
        align-items: center;

        .p-input {
            width: 50px;
            line-height: 18px;
            height: 28px;
            text-align: center;
            box-sizing: border-box;
            border-radius: 3px;
            border: 1px solid #dcdfe6;
            background-color: #fff;
            margin: 0 2px;
            padding: 0 2px;

            &.focus {
                border-color: #409eff;
            }

            .input {
                border: none;
                width: 100%;
                height: 100%;
                background-image: none;
                border-radius: 4px;
                padding: 0;
                text-align: center;
                outline:none;  

                &:focus {
                    border: none;
                }
            }
        }
    }

    .disable {
        color: #c0c4cc !important;
        cursor: no-drop !important;
    }

    .disable-button {
        background: #f4f4f5;
        cursor: no-drop !important;
    }

}
</style>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DevilAngelia

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

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

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

打赏作者

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

抵扣说明:

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

余额充值