Last active
December 21, 2019 11:37
-
-
Save madhusudhan1234/b619201cbd96b284f74c76ba804ea1a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<ul class="pagination"> | |
<li v-if="pagination.current_page > 1"> | |
<a href="javascript:void(0)" aria-label="Previous" v-on:click.prevent="changePage(pagination.current_page - 1)"> | |
<span aria-hidden="true">«</span> | |
</a> | |
</li> | |
<li v-for="page in pagesNumber" :class="{'active': page == pagination.current_page}"> | |
<a href="javascript:void(0)" v-on:click.prevent="changePage(page)">{{ page }}</a> | |
</li> | |
<li v-if="pagination.current_page < pagination.last_page"> | |
<a href="javascript:void(0)" aria-label="Next" v-on:click.prevent="changePage(pagination.current_page + 1)"> | |
<span aria-hidden="true">»</span> | |
</a> | |
</li> | |
</ul> | |
</template> | |
<script> | |
export default{ | |
props: { | |
pagination: { | |
type: Object, | |
required: true | |
}, | |
offset: { | |
type: Number, | |
default: 4 | |
} | |
}, | |
computed: { | |
pagesNumber() { | |
if (!this.pagination.to) { | |
return []; | |
} | |
let from = this.pagination.current_page - this.offset; | |
if (from < 1) { | |
from = 1; | |
} | |
let to = from + (this.offset * 2); | |
if (to >= this.pagination.last_page) { | |
to = this.pagination.last_page; | |
} | |
let pagesArray = []; | |
for (let page = from; page <= to; page++) { | |
pagesArray.push(page); | |
} | |
return pagesArray; | |
} | |
}, | |
methods : { | |
changePage(page) { | |
this.pagination.current_page = page; | |
this.$emit('paginate'); | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment