-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathPagination.vue
More file actions
110 lines (104 loc) · 2.36 KB
/
Copy pathPagination.vue
File metadata and controls
110 lines (104 loc) · 2.36 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
<template>
<div class="row align-items-center justify-content-center bg-light">
<ul class="pagination text-center">
<li class="page-item" @click="previousPage">
<a class="page-link" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
<li
v-for="pageIndex in range"
:key="pageIndex"
:class="{ 'page-item': true, active: pageIndex + startPage == page }"
>
<a class="page-link" @click="page = pageIndex + startPage">{{
pageIndex + startPage
}}</a>
</li>
<li
:class="{ 'page-item': true, disabled: page == pages }"
@click="nextPage"
>
<a class="page-link" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
</ul>
</div>
</template>
<script>
export default {
name: "Pagination",
props: {
pages: {
type: Number,
required: true
}
},
data() {
return {
range: 11,
page: 1,
timer: null
};
},
methods: {
previousPage() {
this.page -= 1;
if (this.page < 1) {
this.page = 1;
}
//this.$emit('fun','previous');
},
nextPage() {
this.page += 1;
if (this.page > this.pages) {
this.page = this.pages;
}
//this.$emit('fun','next');
}
},
watch: {
page(newPage, oldPage) {
if (newPage === oldPage) return;
if(oldPage>newPage)
{
this.$emit('fun','previous','down',oldPage-newPage);
}
if(oldPage<newPage)
{
this.$emit('fun','next','down',newPage-oldPage);
}
clearTimeout(this.timer);
this.timer = setTimeout(() => this.$emit("pagechange", this.page), 0);
}
},
computed: {
startPage() {
if (this.range > this.pages) {
return 0;
}
let range = Math.round(this.range / 2);
let start = this.page - range;
if (start < 0) return 0;
if (start > this.pages || start + this.range > this.pages) {
return this.pages - this.range;
}
return start;
}
},
created() {
if (this.range > this.pages) {
this.range = this.pages;
}
}
};
</script>
<style>
.page {
display: block;
margin: 0 auto;
}
</style>