FleetCart/Themes/Storefront/resources/assets/public/js/components/VPagination.vue

159 lines
4.0 KiB
Vue
Raw Normal View History

2023-06-11 12:14:03 +00:00
<template>
<ul class="pagination">
<li class="page-item" :class="{ disabled: hasFirst }">
<button class="page-link" :disabled="hasFirst" @click="prev">
<i class="las la-angle-left"></i>
</button>
</li>
<li v-show="rangeFirstPage !== 1" class="page-item">
2023-12-03 14:07:47 +00:00
<button class="page-link" @click="goto(1)">1</button>
2023-06-11 12:14:03 +00:00
</li>
<li v-show="rangeFirstPage === 3" class="page-item">
2023-12-03 14:07:47 +00:00
<button class="page-link" @click="goto(2)">2</button>
2023-06-11 12:14:03 +00:00
</li>
<li
2023-12-03 14:07:47 +00:00
v-show="
rangeFirstPage !== 1 &&
rangeFirstPage !== 2 &&
rangeFirstPage !== 3
"
2023-06-11 12:14:03 +00:00
class="page-item disabled"
>
<span class="page-link">...</span>
</li>
<li
v-for="page in range"
:key="page"
class="page-item"
:class="{ active: hasActive(page) }"
>
<button class="page-link" @click="goto(page)">
{{ page }}
</button>
</li>
<li
2023-12-03 14:07:47 +00:00
v-show="
rangeLastPage !== totalPage &&
rangeLastPage !== totalPage - 1 &&
rangeLastPage !== totalPage - 2
"
2023-06-11 12:14:03 +00:00
class="page-item disabled"
>
<span class="page-link">...</span>
</li>
2023-12-03 14:07:47 +00:00
<li v-show="rangeLastPage === totalPage - 2" class="page-item">
2023-06-11 12:14:03 +00:00
<button class="page-link" @click="goto(totalPage - 1)">
{{ totalPage - 1 }}
</button>
</li>
<li v-if="rangeLastPage !== totalPage" class="page-item">
<button class="page-link" @click="goto(totalPage)">
{{ totalPage }}
</button>
</li>
<li class="page-item" :class="{ disabled: hasLast }">
2023-12-03 14:07:47 +00:00
<button
class="page-link"
:class="{ disabled: hasLast }"
@click="next"
>
2023-06-11 12:14:03 +00:00
<i class="las la-angle-right"></i>
</button>
</li>
</ul>
</template>
<script>
2023-12-03 14:07:47 +00:00
export default {
props: {
totalPage: Number,
currentPage: Number,
rangeMax: {
type: Number,
default: 3,
2023-06-11 12:14:03 +00:00
},
2023-12-03 14:07:47 +00:00
},
mounted() {
if (this.currentPage > this.totalPage) {
this.$emit("page-changed", this.totalPage);
}
},
computed: {
rangeFirstPage() {
if (this.currentPage === 1) {
return 1;
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
if (this.currentPage === this.totalPage) {
if (this.totalPage - this.rangeMax < 0) {
2023-06-11 12:14:03 +00:00
return 1;
}
2023-12-03 14:07:47 +00:00
return this.totalPage - this.rangeMax + 1;
}
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
return this.currentPage - 1;
},
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
rangeLastPage() {
return Math.min(
this.rangeFirstPage + this.rangeMax - 1,
this.totalPage
);
},
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
range() {
let rangeList = [];
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
for (
let page = this.rangeFirstPage;
page <= this.rangeLastPage;
page += 1
) {
rangeList.push(page);
}
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
return rangeList;
},
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
hasFirst() {
return this.currentPage === 1;
},
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
hasLast() {
return this.currentPage === this.totalPage;
2023-06-11 12:14:03 +00:00
},
2023-12-03 14:07:47 +00:00
},
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
methods: {
prev() {
this.$emit("page-changed", this.currentPage - 1);
},
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
next() {
this.$emit("page-changed", this.currentPage + 1);
},
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
goto(page) {
if (this.currentPage !== page) {
this.$emit("page-changed", page);
}
},
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
hasActive(page) {
return page === this.currentPage;
2023-06-11 12:14:03 +00:00
},
2023-12-03 14:07:47 +00:00
},
};
2023-06-11 12:14:03 +00:00
</script>