<template>
<div class="pagination">
<button :disabled="mcPage===1" @click="changeCurrentPage(mcPage - 1)">
上一页
</button>
<button v-if="startEnd.start > 1" @click="changeCurrentPage(1)">1</button>
<button v-if="startEnd.start > 2">...</button>
<!-- 中间连续部分 -->
<button
v-for="item in startEnd.end"
v-if="item >= startEnd.start"
:key="item"
@click="changeCurrentPage(item)"
:class="{ active:mcPage==item }"
>
{{item}}
</button>
<button v-if="startEnd.end < totalPages - 1">...</button>
<button
v-if="startEnd.end < totalPages"
@click="changeCurrentPage(totalPages)"
>
{{ totalPages }}
</button>
<button
:disabled="mcPage == totalPages"
@click="changeCurrentPage(mcPage+1)"
>
下一页
</button>
<button style="margin-left: 30px">共{{total}}页</button>
</div>
</template>
<script>
export default {
name: "Pagination",
props: {
currentPage: {
type: Number,
default: 1,
},
pageSize: {
//每一页数量
type: Number,
default: 5,
},
total: {
//总数量
type: Number,
default: 0,
},
showPageNo: {
//连续页码数
type: Number,
default: 5,
},
},
data() {
return {
mcPage: this.currentPage,
};
},
computed: {
totalPages() {
const { total, pageSize } = this;
return Math.ceil(total/pageSize);
},
startEnd() {
//mcPage 当前页码 showPage 连续页码数 totalpages 总页数
//对象解构
const { mcPage, showPageNo, totalPages } = this;
//开始值
let start = mcPage - Math.floor(showPageNo/2);
//1.start不得小于1
if (start < 1) {
start = 1;
}
//结束值
let end = start + showPageNo - 1;
//2.end不得大于总页数
if (end > totalPages) {
end = totalPages
//然后修正start值
start = end - showPageNo + 1
}
if (start < 1) {
start = 1;
}
return { start, end };
},
},
watch: {
//监听currentPage发生改变
currentPage(value) {
this.mcPage = value;
},
},
methods: {
changeCurrentPage(page) {
//修改当前页码
this.mcPage = page;
//通知父组件
this.$emit("currentChange", page);
},
},
};
</script>
<style lang='less' scoped>
.pagination {
button {
margin: 0 5px;
background-color: #f4f4f5;
color: #606266;
outline: none;
border-radius: 2px;
padding: 0 4px;
vertical-align: top;
display: inline-block;
font-size: 13px;
min-width: 35.5px;
height: 28px;
line-height: 28px;
cursor: pointer;
box-sizing: border-box;
text-align: center;
border: 0;
&[disabled] {
color: #c0c4cc;
cursor: not-allowed;
}
&.active {
cursor: not-allowed;
background-color: #409eff;
color: #fff;
}
}
}
</style>
网友评论