分页组件
<template>
<ul class="mo-paging">
<!-- prev -->
<li
:class="['paging-item', 'paging-item--prev', {'paging-item--disabled' : index === 1}]"
@click="prev">prev</li>
<!-- first -->
<li
:class="['paging-item', 'paging-item--first', {'paging-item--disabled' : index === 1}]"
@click="first">first</li>
<li
:class="['paging-item', 'paging-item--more']"
v-if="showPrevMore">...</li>
<li
:class="['paging-item', {'paging-item--current' : index === pager}]"
v-for="pager in pagers"
@click="go(pager)">{{ pager }}</li>
<li
:class="['paging-item', 'paging-item--more']"
v-if="showNextMore">...</li>
<!-- last -->
<li
:class="['paging-item', 'paging-item--last', {'paging-item--disabled' : index === pages}]"
@click="last">last</li>
<!-- next -->
<li
:class="['paging-item', 'paging-item--next', {'paging-item--disabled' : index === pages}]"
@click="next">next</li>
</ul>
</template>
export default {
name : 'MoPaging',
//经由过程props来接收从父组件通报过来的值
props : {
//页面中的可见页码,其他的以...替换, 必需是奇数
perPages : {
type : Number,
default : 5
},
//当前页码
pageIndex : {
type : Number,
default : 1
},
//每页显现条数
pageSize : {
type : Number,
default : 10
},
//总纪录数
total : {
type : Number,
default : 1
},
},
methods : {
prev(){
if (this.index > 1) {
this.go(this.index - 1)
}
},
next(){
if (this.index < this.pages) {
this.go(this.index + 1)
}
},
first(){
if (this.index !== 1) {
this.go(1)
}
},
last(){
if (this.index != this.pages) {
this.go(this.pages)
}
},
go (page) {
if (this.index !== page) {
this.index = page
//父组件经由过程change方法来接收当前的页码
this.$emit('change', this.index)
}
}
},
computed : {
//盘算总页码
pages(){
return Math.ceil(this.size / this.limit)
},
//盘算页码,当count等变化时自动盘算
pagers () {
const array = []
const perPages = this.perPages
const pageCount = this.pages
let current = this.index
const _offset = (perPages - 1) / 2
const offset = {
start : current - _offset,
end : current + _offset
}
//-1, 3
if (offset.start < 1) {
offset.end = offset.end + (1 - offset.start)
offset.start = 1
}
if (offset.end > pageCount) {
offset.start = offset.start - (offset.end - pageCount)
offset.end = pageCount
}
if (offset.start < 1) offset.start = 1
this.showPrevMore = (offset.start > 1)
this.showNextMore = (offset.end < pageCount)
for (let i = offset.start; i <= offset.end; i++) {
array.push(i)
}
return array
}
},
data () {
return {
index : this.pageIndex, //当前页码
limit : this.pageSize, //每页显现条数
size : this.total || 1, //总纪录数
showPrevMore : false,
showNextMore : false
}
},
watch : {
pageIndex(val) {
this.index = val || 1
},
pageSize(val) {
this.limit = val || 10
},
total(val) {
this.size = val || 1
}
}
}
父组件运用
<template>
<div class="list">
<template v-if="count">
<ul>
<li v-for="item in items">...</li>
</ul>
<mo-paging
:page-index="currentPage"
:totla="count"
:page-size="pageSize"
@change="pageChange">
</mo-paging>
</template>
</div>
</template>
<script>
import MoPaging from './paging'
export default {
//显现的声明组件
components : {
MoPaging
},
data () {
return {
pageSize : 20 , //每页显现20条数据
currentPage : 1, //当前页码
count : 0, //总纪录数
items : []
}
},
methods : {
//猎取数据
getList () {
//模仿
let url = `/api/list/?pageSize=${this.pageSize}¤tPage=${this.currentPage}`
this.$http.get(url)
.then(({body}) => {
//子组件监听到count变化会自动更新DOM
this.count = body.count
this.items = body.list
})
},
//从page组件通报过来的当前page
pageChange (page) {
this.currentPage = page
this.getList()
}
},
mounted() {
//要求第一页数据
this.getList()
}
}
</script>