forked from Media24si/vue-pagination
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-bootstrap-pagination.js
112 lines (103 loc) · 3.71 KB
/
vue-bootstrap-pagination.js
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
module.exports = {
template: `<nav>
<ul class="pagination" v-if="pagination.last_page > 0" :class="sizeClass">
<li v-if="showPrevious()" :class="{ 'disabled' : pagination.current_page <= 1 }">
<span v-if="pagination.current_page <= 1">
<span aria-hidden="true">{{ config.previousText }}</span>
</span>
<a href="#" v-if="pagination.current_page > 1 " :aria-label="config.ariaPrevioius" @click.prevent="changePage(pagination.current_page - 1)">
<span aria-hidden="true">{{ config.previousText }}</span>
</a>
</li>
<li v-for="num in array" :class="{ 'active': num === pagination.current_page }">
<a href="#" @click.prevent="changePage(num)">{{ num }}</a>
</li>
<li v-if="showNext()" :class="{ 'disabled' : pagination.current_page === pagination.last_page || pagination.last_page === 0 }">
<span v-if="pagination.current_page === pagination.last_page || pagination.last_page === 0">
<span aria-hidden="true">{{ config.nextText }}</span>
</span>
<a href="#" v-if="pagination.current_page < pagination.last_page" :aria-label="config.ariaNext" @click.prevent="changePage(pagination.current_page + 1)">
<span aria-hidden="true">{{ config.nextText }}</span>
</a>
</li>
</ul>
</nav>`,
props: {
pagination: {
type: Object,
required: true
},
callback: {
type: Function,
required: true
},
options: {
type: Object
},
size: {
type: String
}
},
computed: {
array () {
if (this.pagination.last_page <= 0) {
return [];
}
let from = this.pagination.current_page - this.config.offset;
if (from < 1) {
from = 1;
}
let to = from + (this.config.offset * 2);
if (to >= this.pagination.last_page) {
to = this.pagination.last_page;
}
let arr = [];
while (from <=to) {
arr.push(from);
from++;
}
return arr;
},
config () {
return Object.assign({
offset: 3,
ariaPrevious: 'Previous',
ariaNext: 'Next',
previousText: '«',
nextText: '»',
alwaysShowPrevNext: false
}, this.options);
},
sizeClass () {
if (this.size === 'large') {
return 'pagination-lg';
} else if(this.size === 'small') {
return 'pagination-sm';
} else {
return '';
}
}
},
watch: {
'pagination.per_page' (newVal, oldVal) {
if (+newVal !== +oldVal) {
this.callback();
}
}
},
methods: {
showPrevious () {
return this.config.alwaysShowPrevNext || this.pagination.current_page > 1;
},
showNext () {
return this.config.alwaysShowPrevNext || this.pagination.current_page < this.pagination.last_page;
},
changePage (page) {
if (this.pagination.current_page === page) {
return;
}
this.$set(this.pagination, 'current_page', page);
this.callback();
}
}
};