-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pagination.vue
71 lines (66 loc) · 1.54 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
<template>
<div class="pagination">
<div class="item-list">
<item v-for="(item, idx) in displayItems" :key="idx" :title="item" />
</div>
<div class="page-btns">
<page-button @changePage="changePage" v-for="n in pageNum"
:key="n"
:pageNumber="n"
:curPage="curPage"
/>
</div>
</div>
</template>
<script>
import Item from '@/components/Item.vue';
import PageButton from '@/components/PageButton.vue';
export default {
components: {
Item,
PageButton
},
props: {
items: Array,
itemNumPerPage: Number,
},
data() {
return {
curPage: 1, // curPage starts from 1
pageNum: 0, // number os pages
}
},
created() {
this.calcPageNum();
},
computed: {
displayItems() {
const startIdx = (this.curPage - 1) * this.itemNumPerPage;
const endIdx = startIdx + this.itemNumPerPage;
return this.items.slice(startIdx, endIdx);
},
},
methods: {
changePage(value) {
this.curPage = value;
},
calcPageNum() {
this.pageNum = Math.ceil(this.items.length / this.itemNumPerPage);
}
}
}
</script>
<style scoped lang="scss">
.pagination {
max-width: 640px;
margin: 0 auto;
.item-list {
margin: 5rem 0;
}
.page-btns {
display: flex;
justify-content: center;
align-items: center;
}
}
</style>