vue element-ui后端返回全部数据,前端制作pagination分页功能

由于不可抗拒原因,后端返回全部数据,前端进行分页封装(子页面)

<template>
    <div :class="{ 'hidden': hidden }" class="pagination-container">
        <el-pagination background style="margin-top: 20px; text-align: right;" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" :allData="allData" :current-page="currentPage" :page-sizes="[10,30,50]"
            :page-size="pageSizes" layout="total, sizes, prev, pager, next" :total="total">
        </el-pagination>
    </div>
</template>
<script>
export default {
    name: 'staticPagination',
    data () {
        return {
            currentPage:10,
            pageSizes:1
        }
    },
    props: {
        tableData: {//当前表格的数据
            required: true,
            type: Array
        },
        allData: {//全部的数据
            required: true,
            type: Array
        },
        total: {
            required: true,
            type: Number
        },
        pageIndex: {
            required: true,
            type: Number,
            default: 1
        },
        pageSize: {
            required: true,
            type: Number,
            default: 10
        },
        hidden: {
            type: Boolean,
            default: false
        }
    },
    computed: {
    },
    created () {
        this.pageSizes = this.pageSize
        this.currentPage = this.pageIndex
    },
    methods: {
        getpage () {
            let data = []
            data = this.allData.slice((this.currentPage - 1) * this.pageSizes, this.currentPage * this.pageSizes)
            this.total = this.allData.length
            data.forEach((item, i) => {
                item.$index = (this.currentPage - 1) * this.pageSizes + 1 + i;
            });
            this.$emit('update:tableData', data)//子页面给父页面直接替换el-table的值
        },
        handleSizeChange (val) {//用来切换显示一页几条  
            this.pageSizes = val
            this.getpage()//重新计算给父页面赋值
        },
        // 页数改变
        handleCurrentChange (val) {
            this.currentPage = val
            this.getpage()
        }
    }
}
</script>
  
<style scoped>
.pagination-container {
    background: #fff;
    padding: 32px 16px;
}

.pagination-container.hidden {
    display: none;
}
</style>
  

父页面(使用组件)参数alldata 是全部的数据 tabledata 是当前表格的数据(1/10).sync 非常关键,不然this.$emit('update:tableData', data),改变不了父页面的tabledata 的值

在调用接口的时候执行一次下面这个方法,默认10条数据

最后效果

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值