PagingPlug插件使用手册
插件:
<template>
<nav>
<ul class="pagination" v-show="isTotal">
<li :class="{'disabled': current == 1}"><a href="javascript:;" @click="setCurrent(current - 1)">
<Icon type="arrow-right" size="12" color="#999999"></Icon> </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 href="javascript:;" @click="setCurrent(current + 1)">
<Icon type="arrow-right" size="12" color="#999999"></Icon></a></li>
</ul>
<ul class="pagination" v-show="!isTotal">
<li :class="{'disabled': current == 1}"><a href="javascript:;" @click="setCurrent(1)"> 首页 </a></li>
<li :class="{'disabled': current == 1}"><a href="javascript:;" @click="setCurrent(current - 1)" style="width:1rem"> 上一页 </a></li>
<li ><a class="curPag" style="width:1rem"> 第{{ current }} 页</a></li>
<li :class="{'disabled': current == totalPage}"><a href="javascript:;" @click="setCurrent(current + 1)" style="width:1rem" v-show="isNextVaild"> 下一页 </a></li>
<li :class="{'disabled': current == totalPage}"><a href="javascript:;" style="width:1rem" v-show="!isNextVaild"> 下一页 </a></li>
</ul>
</nav>
</template>
<script type="es6">
export default{
data(){
return {
current: this.currentPage,
}
},
props: {
isNextVaild:{//下一页是否可取
type: Boolean,
default:true
},
isTotal:{//是否有总条数
type: Boolean,
default: true
},
totalPage:{//无总页数时,自己定义的页数
type: Number,
default: 1000
},
total: {// 数据总条数
type: Number,
default: 200000//没有总条数就默认为200000条
},
display: {// 每页显示条数
type: Number,
default: 5
},
currentPage: {// 当前页码
type: Number,
default: 1
},
pagegroup: {// 分页条数
type: Number,
default: 3,
coerce: function (v) {
v = v > 0 ? v : 3;
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);
return list;
}
},
methods: {
setCurrent: function (idx) {
// console.log("总页数"+this.page)
// console.log("虚拟总页数"+this.totalPage)
if(this.isTotal){
if (this.current != idx && idx > 0 && idx < this.page + 1) {
this.current = idx;
this.$emit('pagechange', this.current);
}
}else{
if (this.current != idx && idx > 0 && idx < this.totalPage + 1) {
this.current = idx;
this.$emit('pagechange', this.current);
}
}
}
}
}
</script>
<style lang="less">
.pagination {
overflow: hidden;
display: table;
margin: 0 auto;
/*width: 100%;*/
height: 1rem;
li {
float: left;
height: 0.6rem;
border-radius: 0.1rem;
margin:0.06rem;
color: #666;
&
:hover {
background: rgb(192, 189, 189);
a {
color: #fff;
}
}
a {
display: block;
width: 0.8rem;
height: 0.6rem;
text-align: center;
line-height: 0.6rem;
font-size: 0.24rem;
border-radius: 0.1rem;
text-decoration: none
}
}
.active {
background: rgb(192, 189, 189);
a {
color: #fff;
}
}
}
</style>
情况一:无总条数返回
页码引用:
<template>
<v-PagingPlug ref=”son” :isTotal="isTotal" :totalPage="totalPage" @pagechange="pagechange"></v-pagination>
</template>
<script type="es6">
import PagingPlug from '@/pages/CqPage/PagingPlug';
export default{
data(){
return {
isTotal:false,//是否有总条数
display: 2, // 每页显示条数
current: 1, // 当前的页数
totalPage:1000,//初始化时虚拟总页数,可以作为一个常量,建议不要改变
},
methods: {
pagechange:function(currentPage){
this.search_tydmrz_message(currentPage);
//在 search_tydmrz_message(currentPage)初始时赋值1,
//并在方法中this.current = currentPage;
//This.$refs.son.current = currentPage;
//当出现查询成功无记录时需要
//this.totalPage = currentPage;
}
},
components: {
'v-PagingPlug': PagingPlug,
}
}</script>
情况二:有总条数
<template>
<v-pagination ref=”son” :isTotal="isTotal" :total="total" :display='display' @pagechange="pagechange"></v-pagination>
</template>
<script type="es6">
import PagingPlug from '@/pages/CqPage/PagingPlug';
export default{
data(){
return {
isTotal:true,//是否有总条数
total: 20, // 总条数-需要获取总条数,建议先定义一个值,获取后再修改
display: 2, // 每页显示条数
current: 1 // 当前的页数
},
methods: {
pagechange:function(currentPage){
this.search_tydmrz_message(currentPage);
// ajax请求, 向后台发送 currentPage, 来获取对应的数据
//在 search_tydmrz_message(currentPage)初始时赋值1,
//并在方法中this.current = currentPage;
//This.$refs.son.current = currentPage;
//在发送请求得到总条数total再修改this.total;
//this.total = total;
}
},
components: {
'v-PagingPlug': PagingPlug,
}
}</script>
栗子: