Created
August 29, 2018 07:04
-
-
Save kingpabel/b9260916082904385ea1b9dfbc60f3bc 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
export default { | |
props: { | |
pagination: { | |
type: Object, | |
required: true | |
}, | |
offset: { | |
type: Number, | |
default: 4 | |
} | |
}, | |
template : '<ul class="pagination" v-if="pagination.last_page != 1">\n' + | |
' <li v-if="pagination.current_page > 1">\n' + | |
' <a href="javascript:void(0)" aria-label="Previous" @click.prevent="changePage(pagination.current_page - 1)">\n' + | |
' <span aria-hidden="true">«</span>\n' + | |
' </a>\n' + | |
' </li>\n' + | |
' <li v-for="page in pagesNumber" :class="{\'active\': page == pagination.current_page}">\n' + | |
' <a href="javascript:void(0)" @click.prevent="changePage(page)">{{ page }}</a>\n' + | |
' </li>\n' + | |
' <li v-if="pagination.current_page < pagination.last_page">\n' + | |
' <a href="javascript:void(0)" aria-label="Next" @click.prevent="changePage(pagination.current_page + 1)">\n' + | |
' <span aria-hidden="true">»</span>\n' + | |
' </a>\n' + | |
' </li>\n' + | |
' </ul>', | |
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'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment