VUE简单封装分页器组件

简介: VUE简单封装分页器组件

效果

说在前面

分页器是列表展示页面必备组件之一,由于项目需要,所以我也简单的封装了一个分页器组件,效果如上图。

组件设计

入参
props: {
        //总数
        total: {
            type: Number,
            default: 100,
        },
        //每页条数
        pageSize: {
            type: Number,
            default: 10,
        },
        //数字页码展示个数
        showNum: {
            type: Number,
            default: 3,
        },
        //当前页码
        currentPage: {
            type: Number,
            default: 1,
        },
        //当前页码颜色
        activePageColor: {
            type: String,
            default: '#2A97FF',
        },
    },
变量监听,动态计算展示的页码
watch: {
    currentPage(newVal) {
        this.calPages(newVal);
        this.currentPageT = newVal;
    },
    pageSize() {
        this.calPages(this.currentPage);
    },
},
methods: {
    calPages(newVal) {
        const left = Math.ceil(this.showNum / 2);
        this.startPage =
            left > newVal
              ? 0
              : Math.min(newVal - left, this.getPageNum() - this.showNum);
        this.endPage = Math.min(
            this.getPageNum(),
            this.startPage + this.showNum
        );
    },
    getPageNum() {
        return Math.ceil(this.total / this.pageSize);
    },
}
页码跳转事件
jumpPage(currentPageT) {
    if (currentPageT <= 0 || currentPageT > this.getPageNum()) return;
    this.currentPageT = parseInt(currentPageT);
    this.$emit('currentPageChange', parseInt(currentPageT));
},
完整代码
<template>
    <div class="pagination-item">
        <div class="total">总共{{ getPageNum() }}页,{{ total }}条数据</div>
        <div class="pagination-content">
            <span
                >每页<input
                    v-model="pageSizeT"
                    @change="pageSizeChange(pageSizeT)"
                />条</span
            >
            <span class="btn btn-pre" @click="jumpPage(currentPageT - 1)"
                ><</span
            >
            <span
                v-for="num in endPage - startPage"
                :key="num"
                :style="
                    currentPageT === num + startPage
                        ? 'color:' + activePageColor + ';'
                        : ''
                "
                class="btn btn-num"
                @click="jumpPage(num + startPage)"
                >{{ num + startPage }}</span
            >
            <span class="btn btn-next" @click="jumpPage(currentPageT + 1)"
                >></span
            >
            <span
                ><input v-model="currentPageT" type="number" /><span
                    class="btn btn-jump"
                    @click="jumpPage(currentPageT)"
                    >跳转</span
                ></span
            >
        </div>
    </div>
</template>
<script>
export default {
    name: 'pagination',
    props: {
        //总数
        total: {
            type: Number,
            default: 100,
        },
        //每页条数
        pageSize: {
            type: Number,
            default: 10,
        },
        //数字页码展示个数
        showNum: {
            type: Number,
            default: 3,
        },
        //当前页码
        currentPage: {
            type: Number,
            default: 1,
        },
        //当前页码颜色
        activePageColor: {
            type: String,
            default: '#2A97FF',
        },
    },
    data() {
        return {
            totalT: this.total,
            pageSizeT: this.pageSize,
            currentPageT: '',
            startPage: 0,
            endPage: 0,
        };
    },
    watch: {
        currentPage(newVal) {
            this.calPages(newVal);
            this.currentPageT = newVal;
        },
        pageSize() {
            this.calPages(this.currentPage);
        },
    },
    mounted() {
        this.initData();
    },
    methods: {
        calPages(newVal) {
            const left = Math.ceil(this.showNum / 2);
            this.startPage =
                left > newVal
                    ? 0
                    : Math.min(newVal - left, this.getPageNum() - this.showNum);
            this.endPage = Math.min(
                this.getPageNum(),
                this.startPage + this.showNum
            );
        },
        getPageNum() {
            return Math.ceil(this.total / this.pageSize);
        },
        initData() {
            this.endPage = Math.min(
                this.getPageNum(),
                this.startPage + this.showNum
            );
            if (this.endPage !== 0) this.currentPageT = 1;
        },
        jumpPage(currentPageT) {
            if (currentPageT <= 0 || currentPageT > this.getPageNum()) return;
            this.currentPageT = parseInt(currentPageT);
            this.$emit('currentPageChange', parseInt(currentPageT));
        },
        pageSizeChange(pageSizeT) {
            this.$emit('pageSizeChange', parseInt(pageSizeT));
        },
    },
};
</script>
<style lang="less" scoped>
.pagination-item {
    font-size: 4vw;
    .total {
        text-align: center;
    }
    .pagination-content {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
        input {
            width: 10vw;
            border: rgb(201, 194, 194) 1px solid;
            text-align: center;
        }
    }
}
</style>

组件引用

<template>
    <div class="search-result-page">
        <pagination
            v-if="currentTab !== 3"
            :total="total"
            :page-size="pageSize"
            :show-num="showNum"
            :current-page="currentPage"
            @currentPageChange="currentPageChange"
            @pageSizeChange="pageSizeChange"
        ></pagination>
    </div>
</template>
<script>
import pagination from '../components/pagination.vue';
export default {
    name: 'searchResult',
    components: {
        pagination,
    },
    data() {
        return {
            total: 100,
            showNum: 5,
            pageSize: 10,
            currentPage: 1,
        };
    },
    mounted() { },
    methods: {
        currentPageChange(currentPage) {
            this.currentPage = currentPage;
        },
        pageSizeChange(pageSize) {
            this.pageSize = pageSize;
        },
    },
};
</script>
<style lang="less" scoped></style>

写在后面

更多有趣组件可以查看 http://120.79.163.94/JYeontuComponents/#/homePage

目录
相关文章
|
1天前
|
JavaScript 索引
vue 在什么情况下在数据发生改变的时候不会触发视图更新
vue 在什么情况下在数据发生改变的时候不会触发视图更新
10 2
|
1天前
|
存储 缓存 JavaScript
vue中性能优化
vue中性能优化
|
1天前
|
JavaScript
vue常用指令
vue常用指令
|
1天前
|
JavaScript
vue自定义指令
vue自定义指令
|
1天前
|
JavaScript
vue实现递归组件
vue实现递归组件
10 0
|
1天前
|
缓存 JavaScript
vue 中 keep-alive 组件的作用
vue 中 keep-alive 组件的作用
6 1
|
1天前
|
缓存 JavaScript UED
vue 中的性能优化
vue 中的性能优化
9 2
|
1天前
|
JavaScript 开发者 UED
Vue入门到关门之第三方框架elementui
ElementUI是一个基于Vue.js的组件库,包含丰富的UI组件如按钮、表格,强调易用性、响应式设计和可自定义主题。适用于快速构建现代化Web应用。官网:[Element.eleme.cn](https://element.eleme.cn/#/zh-CN)。安装使用时,需在项目中导入ElementUI和其样式文件。
|
1天前
|
存储 JavaScript 前端开发
vue全家桶详解
vue全家桶详解
8 1
|
1天前
|
JavaScript 前端开发
vue常见的指令
vue常见的指令
9 2