Pagination.vue
3.68 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
<template>
<div class="pagination-container">
<template v-if="rowsTotal > 0">
<nav>
<ul class="pagination light-theme simple-pagination">
<li v-bind:class="{'disabled':isFirst}" @click="prev">
<span class="current prev">Prev</span>
</li>
<li v-for="n in pageChunkCurrent" v-bind:class="{'active':isActive[n+1]}" @click="nav( n+1 )">
<a href="javascript:void(0)">{{n + 1}}</a>
</li>
<li @click="next" v-bind:class="{'disabled':isLast}">
<span class="current next">Next</span>
</li>
</ul>
</div>
</template>
</div>
</template>
<script>
export default {
props: {
pageSize: {
type: Number,
default: 10
},
rowsTotal: {
type: Number,
required: true
},
page: {
type: Number,
required: true
}
},
watch: {
rowsTotal(newVal, oldVal) {
if (newVal !== oldVal) {
// init option when rows changes
this.page = 1
this.isFirst = true
this.isLast = false
}
},
pageTotal(newVal, oldVal) {
if (newVal === 1) {
// only one page
this.isFirst = true
this.isLast = true
}
}
},
computed: {
pageChunkCurrent() {
let pageTotal = Math.ceil(this.rowsTotal / this.pageSize)
let pageArr = []
for (let i = 0; i < pageTotal; i++) {
pageArr[i] = i
}
let pageChunk = []
let j = pageArr.length
for (let i = 0; i < j; i += this.pageSize) {
pageChunk.push(pageArr.slice(i, i + this.pageSize))
}
return pageChunk[this.pageChunkIndex]
},
pageTotal() {
let total = Math.ceil(this.rowsTotal / this.pageSize)
if (total === 1) {
this.isLast = true
this.isFirst = true
}
return Math.ceil(this.rowsTotal / this.pageSize)
},
isActive() {
let pageTotal = this.pageTotal
let o = {}
for (let i = 0; i < pageTotal; i++) {
o[i + 1] = 0
}
o['actived'] = this.page
o[this.page] = 1
return o
}
},
data() {
let pageTotal = this.pageTotal
let pageArr = []
for (let i = 0; i < pageTotal; i++) {
pageArr[i] = i
}
let pageChunk = []
let j = pageArr.length
for (let i = 0; i < j; i += this.pageSize) {
pageChunk.push(pageArr.slice(i, i + this.pageSize))
}
return {
pageChunk: pageChunk,
pageChunkIndex: 0,
isLast: false,
isFirst: true
}
},
methods: {
nav(n) {
let activeObj = this.isActive
activeObj[activeObj['actived']] = 0
activeObj[n] = 1
activeObj['actived'] = n
n === 1 ? this.isFirst = true : this.isFirst = false
this.pageTotal === n ? this.isLast = true : this.isLast = false
this.page = n
this.$dispatch('page', this.page)
},
next() {
let activeObj = this.isActive
if (activeObj.actived % this.pageSize === 0) {
this.pageChunkIndex += 1
this.pageChunkCurrent = this.pageChunk[this.pageChunkIndex]
}
if (activeObj.actived + 1 <= this.pageTotal) {
this.nav(activeObj.actived + 1)
}
},
prev() {
let activeObj = this.isActive
if (activeObj.actived - 1 >= 1) {
this.nav(activeObj.actived - 1)
}
if (activeObj.actived % this.pageSize === 0) {
this.pageChunkIndex -= 1
this.pageChunkCurrent = this.pageChunk[this.pageChunkIndex]
}
}
}
}
</script>