Last active
May 11, 2016 13:12
-
-
Save 599316527/40b059c7c50a3026cc667a4601312079 to your computer and use it in GitHub Desktop.
Vue.js Pagination Component
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> | |
<nav> | |
<ul class="pagination" @click="handleClick($event)"> | |
<li v-if="no > 1"> | |
<a :href="hrefPrefix + (no - 1)" :data-page="no - 1"><</a> | |
</li> | |
<li v-for="i in pageIndicatorSeries" :class="{active: i === no}"> | |
<a :href="hrefPrefix + i" :data-page="i">{{ i }}</a> | |
</li> | |
<li v-if="no < pageCount"> | |
<a :href="hrefPrefix + (no + 1)" :data-page="no + 1">></a> | |
</li> | |
</ul> | |
</nav> | |
</template> | |
<script> | |
// even num | |
const PAGE_INDICATOR_NUMBER = 4 | |
export default { | |
name: 'pagination', | |
props: { | |
size: { | |
type: Number | |
}, | |
no: { | |
type: Number | |
}, | |
total: { | |
type: Number | |
}, | |
trigger: { | |
type: String, | |
default: 'jump' | |
}, | |
hrefPrefix: { | |
type: String, | |
default: '' | |
} | |
}, | |
computed: { | |
pageCount() { | |
return Math.ceil(this.total / this.size) | |
}, | |
pageIndicatorSeries() { | |
let startPage = Math.max(1, this.no - PAGE_INDICATOR_NUMBER / 2) | |
let endPage = startPage + PAGE_INDICATOR_NUMBER | |
if (endPage >= this.pageCount) { | |
endPage = this.pageCount | |
startPage = Math.max(1, endPage - PAGE_INDICATOR_NUMBER) | |
} | |
let series = [] | |
for (let i = startPage; i <= endPage; ++i) { | |
series.push(i) | |
} | |
return series | |
} | |
}, | |
methods: { | |
handleClick(evt) { | |
if (this.trigger !== 'jump') { | |
evt.preventDefault() | |
this.$emit('jump', parseInt(evt.target.dataset.page, 10)) | |
} | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment