Skip to content

Commit c50e114

Browse files
committed
skip calculating pagination range, if all pages can be displayed at once
1 parent b03f6a0 commit c50e114

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/components/Pagination.vue

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
<a class="page-link" href=""> 1 </a>
1717
</li>
1818
<li class="page-item disabled" v-if="start > 3">
19-
<a class="page-link" href="">...</a>
19+
<a class="page-link" href=""></a>
2020
</li>
2121
<li class="page-item" v-for="index in range" :key="index" v-bind:class="{ active: (index == page)}" @click.prevent="pageHandler(index)">
2222
<a class="page-link" href="">{{index}}</a>
2323
</li>
2424
<li class="page-item disabled" v-if="end < totalPages - 2">
25-
<a class="page-link" href="">...</a>
25+
<a class="page-link" href=""></a>
2626
</li>
2727
<li class="page-item" v-if="end < totalPages - 2" @click.prevent="pageHandler(totalPages)">
2828
<a class="page-link" href=""> {{totalPages}} </a>
@@ -31,7 +31,7 @@
3131

3232
<template v-else>
3333
<li class="page-item disabled">
34-
<a class="page-link" href="">...</a>
34+
<a class="page-link" href=""></a>
3535
</li>
3636
</template>
3737
<li :class="{'disabled' : disableNextButton}" class="page-item" @click.prevent="pageHandler(page+1)">
@@ -58,7 +58,7 @@
5858
<!-- Number of rows per page ends here -->
5959

6060
<div class="input-group col-sm-2">
61-
<input type="number" class="form-control" :min="start" :max="totalPages" placeholder="Go to page" @keyup.enter="gotoPage" v-model.number="go_to_page">
61+
<input type="number" class="form-control" min="1" step="1" :max="totalPages" placeholder="Go to page" @keyup.enter="gotoPage" v-model.number="go_to_page">
6262
</div>
6363
</ul>
6464
</nav>
@@ -107,12 +107,7 @@
107107
},
108108
methods: {
109109
gotoPage() {
110-
if (this.go_to_page === "") {
111-
return;
112-
}
113-
114-
if(this.go_to_page < 1 || this.go_to_page > this.totalPages) {
115-
console.log("invalid page number");
110+
if (this.go_to_page === "" || !this.isPositiveInteger(this.go_to_page)) {
116111
return;
117112
}
118113
@@ -128,6 +123,13 @@
128123
this.$emit('update:per_page', option);
129124
},
130125
calculatePageRange(force = false) {
126+
//Skip calculating if all pages can be shown
127+
if (this.totalPages <= this.num_of_visibile_pagination_buttons) {
128+
this.start = 1;
129+
this.end = this.totalPages;
130+
return;
131+
}
132+
131133
//Skip recalculating if the previous and next pages are already visible
132134
if (!force &&
133135
(includes(this.range, this.page - 1) || this.page == 1) &&
@@ -156,6 +158,9 @@
156158
157159
//Handle start underflow
158160
this.start = Math.max(this.start, 1);
161+
},
162+
isPositiveInteger(str) {
163+
return /^\+?(0|[1-9]\d*)$/.test(str);
159164
}
160165
},
161166
components: {

0 commit comments

Comments
 (0)