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

目录
相关文章
|
4月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
418 2
|
3月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
373 137
|
6月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
537 1
|
6月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
350 0
|
7月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
903 0
|
9月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
1179 4
|
7月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
8月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
1121 78
|
9月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍
|
7月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
546 17