分頁組件實現 ,使用element 的pagination組件進行再一次封裝
https://element.eleme.io/#/zh-CN/component/pagination#
<template>
<div :class="{'hidden':hidden}" class="pagination-container">
<el-pagination
:background="background"
:current-page.sync="currentPage"
:pag-size.sync="pageSize"
:layout="layout"
:page-sizes="pageSizes"
:total="total"
v-bind="$attrs"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>
使用計算屬性更改pageSize 改變時會觸發 事件size-change或者currentPage 改變時會觸發current-change
computed: {
currentPage: {
get() {
return this.page
},
set(val) {
this.$emit('update:page', val)
}
},
pageSize: {
get() {
return this.limit
},
set(val) {
this.$emit('update:limit', val)
}
}
},
當改變pageSize或者currentPage 觸發size-change或者current-change 綁定事件
methods:{
handleSizeChange(val) {
this.$emit('pagination', { page: this.currentPage, limit: val })
if (this.autoScroll) {
this.pageSize = val
scrollTo(0, 800)
}
},
handleCurrentChange(val) {
// this.$emit('update:page', val)
// this.$emit('update:limit', this.pageSize)
this.$emit('pagination', { page: val, limit: this.pageSize })
if (this.autoScroll) {
scrollTo(0,800)
}
}
}
//在使用的地方引用並使用
import Pagination from '@/components/Pagination'
<pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData"></pagination>