前端翻页组件
/*
*翻页计算规律:
*开始:(当前页码-1) * 每页显示的条数
*结束:当前页码 * 每页显示的条数
*/
<template>
<nav>
<ul class="pagination">
<li class='totalPage'>共{{totalNum}}页</li>
<li :class="{'disabled':current == 1}"><a class="arrow" href="javascript:;" @click="setCurrent(current - 1)"> < </a></li>
<!-- <li :class="{'disabled': current == 1}"><a href="javascript:;" @click="setCurrent(1)"> 首页 </a></li> -->
<li v-for="(p,i) in grouplist" :key="i" :class="{'active':current == p.val}"><a href="javascript:;" @click="setCurrent(p.val)"> {{ p.text }} </a></li>
<!-- <li :class="{'disabled': current == page}"><a href="javascript:;" @click="setCurrent(page)"> 尾页 </a></li> -->
<li :class="{'disabled': current == page}"><a class="arrow" href="javascript:;" @click="setCurrent(current + 1)"> ></a></li>
</ul>
</nav>
</template>
<script type="es6">
export default{
data(){
return {
current: this.currentPage
}
},
props: {
total: {// 数据总条数
type: Number,
default: 0
},
totalNum:{
type:Number,
default:1
},
display: {// 每页显示条数
type: Number,
default: 1
},
currentPage: {// 当前页码
type: Number,
default: 1
},
pagegroup: {// 分页条数
type: Number,
default: 1,
coerce: function (v) {
v = v > 0 ? v : 5;
return v % 2 === 1 ? v : v + 1;
}
}
},
computed: {
page: function () { // 总页数
return Math.ceil(this.total / this.display);
},
grouplist: function () { // 获取分页页码
var len = this.page, temp = [], list = [], count = Math.floor(this.pagegroup / 2), center = this.current;
if (len <= this.pagegroup) {
while (len--) {
temp.push({text: this.page - len, val: this.page - len});
};
return temp;
}
while (len--) {
temp.push(this.page - len);
};
var idx = temp.indexOf(center);
(idx < count) && ( center = center + count - idx);
(this.current > this.page - count) && ( center = this.page - count);
temp = temp.splice(center - count - 1, this.pagegroup);
do {
var t = temp.shift();
list.push({
text: t,
val: t
});
} while (temp.length);
if (this.page > this.pagegroup) {
(this.current > count + 1) && list.unshift({text: '...', val: list[0].val - 1});
(this.current < this.page - count) && list.push({text: '...', val: list[list.length - 1].val + 1});
}
return list;
}
},
methods: {
setCurrent: function (idx) {
console.log(44)
if (this.current != idx && idx > 0 && idx < this.page + 1) {
this.current = idx;
this.$emit('pagechange', this.current);
}
}
}
}
</script>
<style lang="less">
.pagination {
overflow: hidden;
display: table;
padding-left: 0;
margin: 0 auto;
/*width: 100%;*/
li {
list-style: none;
float: left;
border-radius: 5px;
min-width: 20px;
height: 30px;
line-height: 30px;
margin: 3px 5px;
border:none;
color: #666;
&:hover {
.arrow{
border: 1px solid #ccc;
color: #999;
}
}
a {
display: inline-block;
box-sizing: border-box;
text-align: center;
font-size: 12px;
border-radius: 5px;
width: 100%;
height: 20px;
line-height: 20px;
text-decoration: none;
color: #999;
border: 1px solid #ccc;
}
&.arrow{
border: 1px solid #ccc;
color: #999;
}
&:hover{
color: #999;
}
&.totalPage{
border:none;
background: #fff;
color: #333;
}
}
&
.active {
a {
color: #fff;
background-color: #096DDD;
border: none;
}
}
}
</style>
使用示例:
<template>
<div class="hello">
<div v-for="(v,i) in list" :key=i>
{{v}}
</div>
<div id="dd"></div>
<pagin-taion :total='total' :totalNum='totalNum' :display='display' :pagegroup='pagegroup' @pagechange='setgal'></pagin-taion>
</div>
</template>
<script>
import paginTaion from '@/gg/paginTaion';
export default {
name: 'HelloWorld',
data () {
return {
total:null, // 总数
totalNum:null, // 总的分页数
display:2, // 每页显示的条数
pagegroup:3,// 页码展示的数量(序号数量)
currentPage:1, //当前页码
list:[], // 用于展示的数据
pageData:['1','2','3','4','5','6','7','8','9','10','11','12','13','14','16','17','18','19','20'] // 全部的数据
}
},
created(){
this.setgal(1)
},
components:{
paginTaion,
},
methods:{
setgal(page){
//this.initHtmls(page);
this.currentPage = page;
this.total = this.pageData.length;
this.totalNum = Math.ceil(this.total/this.display);
// 获取当前页,然后从数组pageData中去取数据,然后更新list中的数据
this.list = this.pageData.slice((page-1) * this.display, page * this.display);
}
}
}
</script>
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>