uniapp 分页组件封装

先上图
在这里插入图片描述
由于项目中使用分页组件需要有页码展示,并且能够点击跳转,本想使用uni-ui官方组件库来自定义,但是uni-ui官方组件库的分页组件实在是太过粗糙,故此自行封装。

官方分页器:
在这里插入图片描述

前提===>保证项目安装有uni-ui组件库

gitee联系作者:https://gitee.com/zlflying
<template>
    <!-- 分页组件 -->
    <view class="page">
        <view class="left-btn" :class="currentPage === 1 ? 'disable' : ''"
            @click="() => { if (currentPage !== 1) { changePage(currentPage - 1) } }">
            <uni-icons type="left" size="22" color="black"></uni-icons>
        </view>
        <view class="page-num" id="page-num">
            <view class="num" :class="currentPage == item ? 'active' : ''" v-for="(item, index) in currentPageList"
                @click="changePage(item)">
                {{ item }}
            </view>
            <view class="num" v-if="currentPage < totalPage" @click="changePage(currentPageList[pageSize - 1] + 1)">
                ...
            </view>
        </view>
        <view class="right-btn" :class="currentPage === totalPage ? 'disable' : ''"
            @click="() => { if (currentPage !== totalPage) { changePage(currentPage + 1) } }">
            <uni-icons type="right" size="22" color="black"></uni-icons>
        </view>
    </view>
</template>
<script>
export default {
    props: {
        //总页码
        totalPage: {
            type: Number,
            default: 0
        },
        // 每页数量
        pageSize: {
            type: Number,
            default: 5
        },
    },
    computed: {
        // 显示页码
        currentPageList() {
            // 序列生成器
            const range = (start, stop, step) =>
                Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);
            if (this.currentPage > this.pageSize) {
                return range(this.currentPage - this.pageSize + 1, this.currentPage, 1);
            } else {
                return range(1, this.pageSize, 1);
            }
        }
    },
    data() {
        return {
            //当前页码
            currentPage: 1,
        }
    },
    methods: {
        changePage(page) {
            this.currentPage = page;
            this.$emit('changePage', this.currentPage);
        }
    }
}
</script>
<style scoped lang="scss">
.page {
    width: 90%;
    margin: 10px auto;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .left-btn {
        background-color: #fff;
        border-radius: 5px;
        padding: 5px;
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    }

    .page-num {
        width: 60%;
        display: flex;
        justify-content: space-between;
        overflow: hidden;

        .num {
            background-color: #fff;
            padding: 3px 7px;
            border-radius: 5px;
            font-size: 17px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }
    }

    .right-btn {
        background-color: #fff;
        border-radius: 5px;
        padding: 5px;
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    }

}

.active {
    background-color: #388EFC !important;
    color: #fff;
}

.disable {
    background-color: #ccc !important;
    cursor: not-allowed;
}
</style>

使用

<template>
<pageView class="page" :totalPage="page" @changePage="changePage"></pageView>
</template>
<script>
import pageView from "@/components/common/pageView/index.vue";
export default {
	components: {
		pageView
	},
	data() {
		return {
			page:15
		}
	},
	methods: {
		changePage(page) {
			console.log(page);
		}
	}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值