-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPagination.vue
87 lines (76 loc) · 1.76 KB
/
Pagination.vue
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
<template>
<nav class="codeweek-pagination" role="navigation" aria-label="pagination">
<ul>
<li>
<a
class="back"
@click.prevent="changePage(pagination.current_page - 1)"
:disabled="pagination.current_page <= 1"
>
{{ $t('pagination.previous') }}
</a>
</li>
<li v-for="page in pages">
<a
v-if="pagination.current_page != page"
class="page"
@click.prevent="changePage(page)"
>
{{ page }}
</a>
<a v-else class="page current">
{{ page }}
</a>
</li>
<li>
<a
class="next"
@click.prevent="changePage(pagination.current_page + 1)"
:disabled="pagination.current_page >= pagination.last_page"
>
{{ $t('pagination.next') }}
</a>
</li>
</ul>
</nav>
</template>
<style>
.pagination {
margin-top: 40px;
}
</style>
<script>
export default {
props: ['pagination', 'offset'],
methods: {
isCurrentPage(page) {
return this.pagination.current_page === page;
},
changePage(page) {
if (page < 1 || page > this.pagination.last_page) {
return;
}
this.pagination.current_page = page;
this.$emit('paginate', page); // Emit the event with the new page number
}
},
computed: {
pages() {
let pages = [];
let from = this.pagination.current_page - Math.floor(this.offset / 2);
if (from < 1) {
from = 1;
}
let to = from + this.offset - 1;
if (to > this.pagination.last_page) {
to = this.pagination.last_page;
}
while (from <= to) {
pages.push(from);
from++;
}
return pages;
}
}
};
</script>