pagination.vue
5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<template>
<div class="btn-group pagination" role="group" aria-label="Pagination">
<button type="button"
:class="['btn','btn-'+secondaryVariant,btnSize]"
:disabled="currentPage == 1 "
@click.prevent="(currentPage == 1) ? _return : currentPage--">
<span aria-hidden="true">上一页</span>
</button>
<button type="button"
:class="['btn','btn-'+secondaryVariant,btnSize,currentPage === 1 ? 'active' : '']"
@click.prevent="currentPage = 1"
v-show="showPrev">1
</button>
<span :class="['btn','btn-'+secondaryVariant,btnSize]" v-show="showPrev">...</span>
<button type="button"
:class="['btn',
btnSize,
btnVariant(index),
index + diff === currentPage ? 'active' : '',
index + diff !== currentPage ? 'hidden-xs-down' : '']"
v-for="(item,index) in pageLinks"
@click.prevent="currentPage = index + diff">{{index + diff}}
</button>
<span :class="['btn','btn-'+secondaryVariant,btnSize]" v-show="showNext">...</span>
<button type="button"
:class="['btn','btn-'+secondaryVariant,btnSize,numberOfPages === currentPage ? 'active' : '']"
v-show="showNext"
@click.prevent="currentPage = numberOfPages">{{numberOfPages}}
</button>
<button type="button"
:class="['btn','btn-'+secondaryVariant,btnSize]"
:disabled="currentPage == numberOfPages"
@click.prevent="(currentPage == numberOfPages) ? _return : currentPage++">
<span aria-hidden="true">下一页</span>
</button>
</div>
</template>
<script>
export default {
data() {
return {
diff: 1,
showPrev: false,
showNext: false,
currentPage: this.value
};
},
computed: {
numberOfPages() {
const result = Math.ceil(this.totalRows / this.perPage);
return (result < 1) ? 1 : result;
},
btnSize() {
return !this.size || this.size === `default` ? `` : `btn-${this.size}`;
},
pageLinks() {
let result = this.limit;
if (this.currentPage > this.numberOfPages) {
this.currentPage = 1;
}
this.diff = 1;
this.showPrev = false;
this.showNext = false;
// if less pages than limit just show this pages
if (this.numberOfPages <= this.limit) {
return this.numberOfPages;
}
// if at the beggining of the list or at the end show full number of pages within limit - 2
// -2 is reserves space for two buttons: "..." and "first/last button"
if (this.currentPage <= this.limit - 2) {
this.diff = 1;
this.showNext = true;
result = this.limit - 2;
}
// at the end of the range
if (this.currentPage > this.numberOfPages - this.limit + 2) {
this.diff = this.numberOfPages - this.limit + 3;
this.showPrev = true;
result = this.limit - 2;
}
// if somehere in the middle show just limit - 4 links in the middle and one button on the left with "..." and on button on the right preceeded with "..."
if (this.currentPage >= this.limit - 2 && this.currentPage <= this.numberOfPages - this.limit + 2) {
this.diff = this.currentPage - 1;
this.showPrev = true;
this.showNext = true;
result = this.limit - 4;
}
return result;
}
},
methods: {
btnVariant(index) {
return (index + this.diff === this.currentPage) ? `btn-${this.variant}` : `btn-${this.secondaryVariant}`;
},
_return() {
}
},
watch: {
currentPage(newPage) {
this.$emit('input', newPage);
},
value(newValue, oldValue) {
if (newValue !== oldValue) {
this.currentPage = newValue;
}
}
},
props: {
value: {
type: Number,
default: 1
},
limit: {
type: Number,
default: 7
},
perPage: {
type: Number,
default: 20
},
totalRows: {
type: Number,
default: 20
},
size: {
type: String,
default: 'md'
},
variant: {
type: String,
default: 'primary'
},
secondaryVariant: {
type: String,
default: 'default'
}
}
};
</script>