因elementUi分頁封裝成單獨組件當前頁存在bug,沒有在父組件設(shè)置當前頁。 如有需要可在分頁組件單獨設(shè)置
在使用頁面(父頁面)
<!-- 向子組件傳total值(總數(shù)),接收子組件sizeChange事件(每頁顯示條數(shù)),接收子組件currentChange事件(當前頁) -->
<paging
:total="querycdt.total"
@sizeChange="handleSizeChange"
@currentChange="handleCurrentChange">
</paging>
components:{
paging : require('@/components/common/paging.vue').default, // 引入分頁組件
}
// 每頁顯示多少條
handleSizeChange(val){
this.querycdt.pageLength = val // 每頁多少條
this.getUserList() // 刷新
},
// 當前頁
handleCurrentChange(val) {
this.querycdt.pagenum = val // 當前頁
this.getUserList() // 刷新
}
在 paging 組件頁面
<template>
<div>
<el-pagination
@size-change='handleSizeChange'
@current-change="handleCurrentChange"
:page-sizes="[10,20,30]"
:page-size="10"
layout="total, prev, pager, next, jumper"
:total="querycdt.total">
</el-pagination>
</div>
</template>
<script>
export default {
props: ['total'],
methods:{
handleSizeChange(val){
// console.log(`每頁${val}條`)
// 向父組件傳遞sizeChange事件,傳入val值
this.$emit('sizeChange',val)
},
handleCurrentChange(val){
// console.log(`當前頁:${val}`)
this.$emit('currentChange',val)
}
}
}
</script>
<style lang="less" scoped>
</style>
刪除頁面最后一條數(shù)據(jù)時,currentPage沒有減一,頁面列表為空
思路:監(jiān)聽頁面的總條數(shù),并對總條數(shù)進行判斷。
當頁面總條數(shù) = (當前頁數(shù)-1)*當前頁條數(shù),currentPage減1,重新獲取列表
props: ['querycdt'], // 直接接收父組件傳過來的querycdt分頁對象
// querycdt.totalNum 數(shù)據(jù)總條數(shù)
// querycdt.currentPage 當前頁
// querycdt.pageSize 每頁顯示的數(shù)據(jù)條數(shù)
watch: {
'querycdt.currentPage':{ //監(jiān)聽props傳過來的querycdt對象的currentPage(當前頁)
handler(){ // 隨便寫在一個函數(shù)內(nèi)
if(this.querycdt.totalNum==(this.querycdt.currentPage-1)*this.querycdt.pageSize&& this.querycdt.totalNum!=0){ //這一行是關(guān)鍵代碼,倍數(shù)關(guān)系
this.querycdt.currentPage -= 1
this.geiList()//獲取表格數(shù)據(jù)的方法
}
}
}
},
額外說一點,數(shù)據(jù)的每個序號,一般用
item.index= this.querycdt.pageSize * (this.querycdt.currentPage - 1) + (index + 1);