Skip to content

Commit 9445e2a

Browse files
committed
增加分页组件
1 parent 1bdee6d commit 9445e2a

File tree

5 files changed

+142
-22
lines changed

5 files changed

+142
-22
lines changed

src/components/listItemBox.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ export default {
3232
}
3333
}
3434
},
35-
updated(){
36-
console.log(com.formatDate(this.data[0].last_reply_at))
37-
},
3835
methods:{
3936
formatDate:(time) => com.formatDate(time),
4037
}

src/components/pages.vue

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/components/pagination.vue

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<template>
2+
<div class="pagination">
3+
<ul class="pager">
4+
<li class="btn-prev" :class="{'disabled': currentNum === 1}" @click="goNum(currentNum-1)"><</li>
5+
<li v-if="showMorePrev" class="quickprev" @click="goNum(currentNum-5)">...</li>
6+
<li class="number" v-for="pager in pagers" :class="{'active': pager == currentNum }" @click="goNum(pager)">
7+
{{pager}}
8+
</li>
9+
<li v-if="showMoreNext" class="quicknext" @click="goNum(currentNum+5)">...</li>
10+
<li class="btn-prev" :class="{'disabled': currentNum === page}" @click="goNum(currentNum-1)">></li>
11+
</ul>
12+
</div>
13+
</template>
14+
<script>
15+
export default {
16+
props:{
17+
currentPage: {
18+
type: Number,
19+
default: 1
20+
},
21+
perPages: {
22+
type:Number,
23+
default: 5
24+
},
25+
pageSize: {
26+
type:Number,
27+
default:10
28+
},
29+
total:Number,
30+
31+
},
32+
data(){
33+
return {
34+
currentNum: this.currentPage,
35+
showMorePrev:false,
36+
showMoreNext:false,
37+
}
38+
},
39+
computed:{
40+
page(){
41+
return Math.ceil(this.total/ this.pageSize );
42+
},
43+
pagers(){
44+
const arr=[];
45+
const perPages = this.perPages,page=this.page, center = (perPages-1)/2;
46+
let currentPage=this.currentNum, start=currentPage-center,end=currentPage+center;
47+
48+
if( start < 1 ){
49+
end = end + (1-start);
50+
start = 1;
51+
}
52+
53+
if( end > page ){
54+
end = page;
55+
start =start - (end -page);
56+
}
57+
58+
if( start < 1) start =1;
59+
60+
for(let i =start ;i<=end;i++){
61+
arr.push(i);
62+
}
63+
64+
this.showMorePrev = (start > 1);
65+
this.showMoreNext = (end < page);
66+
67+
return arr;
68+
69+
}
70+
},
71+
methods:{
72+
goNum:function(num){
73+
const pageNum = this.currentNum;
74+
this.currentNum = num;
75+
if(this.currentNum<1) this.currentNum=1;
76+
if(this.currentNum>this.page) this.currentNum = this.page;
77+
if(this.currentNum ==pageNum) return;
78+
this.$emit('updata', this.currentNum);
79+
}
80+
}
81+
}
82+
</script>
83+
<style lang="less" scoped>
84+
.pagination {
85+
font-size: 0;
86+
white-space: nowrap;
87+
color: #48576a;
88+
.btn-prev,.btn-next{
89+
border: 1px solid #d1dbe5;
90+
}
91+
.btn-prev{
92+
border-radius: 2px 0 0 2px;
93+
}
94+
.btn-next{
95+
border-radius: 0 2px 2px 0;
96+
}
97+
.disabled{
98+
color: #e4e4e4;
99+
background-color: #fff;
100+
cursor: not-allowed;
101+
}
102+
}
103+
.pager{
104+
display: inline-block;
105+
vertical-align: top;
106+
li{
107+
display: inline-block;
108+
padding: 0 4px;
109+
border: 1px solid #d1dbe5;
110+
border-right: 0;
111+
background: #fff;
112+
font-size: 13px;
113+
min-width: 28px;
114+
height: 28px;
115+
line-height: 28px;
116+
cursor: pointer;
117+
box-sizing: border-box;
118+
text-align: center;
119+
&.active{
120+
border-color: #20a0ff;
121+
background-color: #20a0ff;
122+
color: #fff;
123+
cursor: default;
124+
}
125+
}
126+
}
127+
</style>

src/style/common.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ ul{
4848
padding: 10px;
4949
background-color: #f6f6f6;
5050
}
51+
.inner{
52+
background: #fff;
53+
}
5154
}
5255

5356
.user_avatar img, .user_big_avatar img {

src/view/index/index.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
</div>
66
<div class="inner">
77
<listItemBox :data="listData"></listItemBox>
8+
<pagination @updata="clickPage" :current-page="reqData.page" :total="800"></pagination>
89
</div>
910
</div>
1011
</template>
1112

1213
<script>
1314
import listItemBox from "@/components/listItemBox";
15+
import pagination from "@/components/pagination";
1416
import * as api from "@/api/api";
1517
1618
export default {
@@ -25,19 +27,27 @@ export default {
2527
{name:"客户端测试", query:"dev"},
2628
],
2729
listData:[],
30+
reqData:{
31+
page: 1,
32+
tab: this.$route.query.tab
33+
}
2834
}
2935
},
30-
components:{ listItemBox },
36+
components:{ listItemBox, pagination },
3137
created(){
3238
this.getData();
3339
},
3440
methods:{
3541
getData: function(){
36-
api.getTopicsList({tab:this.$route.query.tab}).then(res=>{
42+
api.getTopicsList(this.reqData).then(res=>{
3743
if(res.success){
3844
this.listData=res.data;
3945
}
4046
})
47+
},
48+
clickPage:function(num){
49+
this.reqData.page=num;
50+
this.getData();
4151
}
4252
},
4353
watch:{

0 commit comments

Comments
 (0)