Element + Vue之封装的分页组件

调用该组件需要传以下几个参数

  1. 一种是数据量少的时候,后端将全部数据给到你,你来进行分页功能 需要将全部数据传过来 pageData
  2. 进行选择设置每页条数 pageCurrent
  3. 每页显示的条数 pagesize
  4. 页码 b_current
  5. 后台写分页功能的时候,需要给你传过来数据的总条数,用于展示和计算数据条数和当前页数的计算 total
<template>
    <div class="block">
        <div class="page-desc">{{total?total:pageData.length}}条记录 第{{current}}/{{toatPage}}</div>
        <div class="block-page">
            <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="current"        
            :page-sizes="pageCurrent"
            :page-size="pageSizes"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total?total:pageData.length"
            @prev-click="prevClick"
            @next-click="nextClick">
            </el-pagination>
        </div>
    </div>
</template>

<script>
export default {
    name: 'page',
    props: ['pageData','pageCurrent','pagesize','b_current','total'],
    data(){
        return {
            pageSizes: this.pagesize,          // 每页显示条数
            current:   this.b_current          // 页码
        }
    },
    computed: {
    	// 计算总页数
        toatPage(){
            if(this.total) {
                let totalPage=Math.ceil(this.total/this.pageSizes);
                return totalPage
            }else {
                let totalPage=Math.ceil(this.pageData.length/this.pageSizes);
                return totalPage
            }
        }
    },
    methods:{
        handleSizeChange(val) {         // 显示条数改变时触发
            this.pageSizes = val
        },
        handleCurrentChange(val) {      // 页码变化时触发
            this.current = val
        },
        // 上一页
        prevClick(){
            this.current=this.current-1
            this.$emit('getargument',{pagesize: this.pageSizes,current: this.current})
        },
        // 下一页
        nextClick(){
            this.current=this.current+1
            this.$emit('getargument',{pagesize: this.pageSizes,current: this.current})
        }
    },
    created(){
        this.$emit('getargument',{pagesize: this.pageSizes,current: this.current})
    },
    watch: {
    	// 监听调整条数、页码、每页展示条数的变化,并将改变后的值返回给父组件
        pageSizes(val){
            this.$emit('getargument',{pagesize: this.pageSizes,current: this.current})
        },
        current(val){
            this.$emit('getargument',{pagesize: this.pageSizes,current: this.current})
        },
        b_current(val) {
            this.current = val
        }
    },
    
}
</script>

<style lang="less" scoped>
/* 设置分页组件的一些响应式问题 */
.block {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    
    .page-desc {
        margin-top: 20px;
        height: 28px;
        line-height: 28px;
        font-size: 12px;
        vertical-align: top;
        color: #606266;
    }
}
.el-pagination {
    margin-top: 20px;
    text-align: right;
    display: flex;
    flex-wrap: wrap;
}
/deep/.el-pager {
    display: flex;
    flex-wrap: wrap;
}

@media (max-width: 768px) {
    .block-page {
        width: 100%;
        
    }
    .el-pagination {
        text-align: left;
    }
    .page-desc {
        width: 100%;
        padding: 0 10px;
    }
    /deep/.el-pagination__sizes {
        display: block !important;
        width: 100% !important;
        margin-bottom: 20px;
    }
    
}
@media (max-width: 820px) {
    /deep/.el-pagination__total {
        display: none !important;
    }
    
}
@media (max-width: 450px) {
    /deep/.el-pagination__jump {
        display: block !important;
        width: 100% !important;
        margin-top: 20px;
    }
    /deep/.el-message-box {
        width: 73% !important;
    }
}
</style>

调用该组件

<page :pageData="keywordListData" :pageCurrent = "[10,20,50,100]" :total="total" :pagesize="pageSize" :b_current="b_current" v-on:getargument="getargument" />

v-on:getargument 接收组件返回的值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不会做饭的程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值