参数说明
pageNo:当前是第几页
pageSize:一页展示多少条数据
total 一共有多少条数据
continues :代表连续分页码是多少,一般是5或者7
<template>
<div class="pagination">
<button @click="previousPage">上一页</button>
<button
:class="{ cur: pageNo === 1 }"
@click="changePage(1)"
v-if="startNumAndEndNum.startNum != 1"
>
1
</button>
<button
v-if="startNumAndEndNum.startNum != 1 && startNumAndEndNum.startNum != 2"
>
···
</button>
<!-- 中间部分 -->
<button
v-for="(page, index) in startNumAndEndNum.endNum"
:key="index"
v-if="page >= startNumAndEndNum.startNum"
:class="{ cur: pageNo === page }"
@click="changePage(page)"
>
{{ page }}
</button>
<button
v-if="
startNumAndEndNum.endNum != totalPage &&
startNumAndEndNum.endNum != totalPage - 1
"
>
···
</button>
<button
@click="changePage(totalPage)"
v-if="startNumAndEndNum.endNum != totalPage"
>
{{ totalPage }}
</button>
<button @click="nextPage">下一页</button>
<button style="margin-left: 30px">共 {{ totalPage }} 页</button>
</div>
</template>
<script>
export default {
data() {
return {
continues: 5,
pageSize:10,
pageNo:1,
total:91,
};
},
//props: ["pageSize", "pageNo", "total"], // 这是从父组件传递过来的参数
computed: {
// 计算总共有多少页
totalPage() {
// 向上取整
return Math.ceil(this.total / this.pageSize);
},
// 计算连续页码的起始数据以及结束数据
startNumAndEndNum() {
let startNum = 0;
let endNum = 0;
// 如果总页码数小于continue
if (this.totalPage <= this.continue) {
startNum = 1;
endNum = this.totalPage;
} else {
startNum = this.pageNo - parseInt(this.continue / 2);
endNum = this.pageNo + parseInt(this.continue / 2);
if (startNum < 1) {
startNum = 1;
endNum = this.continue;
}
if (this.totalPage < endNum) {
endNum = this.totalPage;
startNum = this.totalPage - this.continue + 1;
}
}
return { startNum, endNum };
},
},
methods: {
changePage(page) {
// this.$emit("changeCurrentPage", page); //发送给父组件
this.pageNo=page
},
previousPage() {
if (this.pageNo != 1) {
// this.$emit("previousPage");
this.pageNo -=1;
}
},
nextPage() {
if (this.pageNo != this.totalPage) {
//this.$emit("nextPage");
this.pageNo +=1
}
},
},
};
</script>
<style lang="less" scoped>
.pagination {
text-align: center;
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;
}
}
.cur {
background-color: #e1251b;
color: #fff;
}
}
</style>
样式: