Vue.js Pagination Component With Bootstrap
February 7, 2020Setup Bootstrap 4 or BootstrapVue.
Pagination Component
Vue.js Component
<template>
<ul class="pagination">
<li class="page-item" :class="{'disabled': currentPage == 1}">
<a class="page-link" :href="pageUrl(1)">First</a>
</li>
<li class="page-item" :class="{'disabled': currentPage == 1}" >
<a class="page-link" :href="pageUrl(currentPage-1)" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
<li class="page-item" :class="{'active': currentPage == index, 'disabled': index == '...'}" v-for="(index, key) in pages" :key="key">
<a class="page-link" :href="pageUrl(index)">{{ index }}</a>
<!-- <router-link class="page-link" :to="pageUrl(index)">{{ index }}</router-link> -->
</li>
<li class="page-item" :class="{'disabled': currentPage == totalPage}">
<a class="page-link" :href="pageUrl(currentPage+1)" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
<li class="page-item" :class="{'disabled': currentPage == totalPage}">
<a class="page-link" :href="pageUrl(totalPage)">Last</a>
</li>
</ul>
</template>
<script>
export default {
name: 'pagination',
props: {
currentPage: Number,
totalPage: Number
},
/*
data() {
return {
currentPage: 6,
totalPage: 6
}
},
*/
computed: {
pages() {
let nums = []
nums.push(this.currentPage)
let count = 1
let index = this.currentPage - count
while (index >= 1) {
nums.unshift(index)
count++
index = this.currentPage - count
if (count > 2) break
}
if (index >= 1) nums.unshift('...')
count = 1
index = this.currentPage + count
while (index <= this.totalPage) {
nums.push(index)
count++
index = this.currentPage + count
if (count > 2) break
}
if (index <= this.totalPage) nums.push('...')
return nums
}
},
methods: {
pageUrl(index) {
if (index == 1) {
return '/'
}
else if (index < 1 || index > this.totalPage) {
return ''
}
else {
return `/page/${index}`
}
}
}
}
</script>
NOTE: If you are using Vue-Router, you might want to use <router-link class="page-link" :to="pageUrl(index)">{{ index }}</router-link>
instead of <a class="page-link" :href="pageUrl(index)">{{ index }}</a>
Usage
<template>
<div>
<pagination :total-page="totalPage" :current-page="page" />
</div>
</template>
<script>
import Pagination from '@/components/Pagination.vue'
export default {
name: 'home',
data() {
return {
totalPage: 6
page: 1
}
},
components: {
Pagination
}
}
</script>
❤️ Is this article helpful?
Buy me a coffee☕ or support my work to keep this space 🖖 and ad-free.
If you can't, do send some 💖 to @d_luaz or help to share this article.
If you can't, do send some 💖 to @d_luaz or help to share this article.
👶 Apps I built
Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.Pixtory App (Alpha) - easily organize photos on your phone into a blog.
暖心芽 (WIP) 🌞❤️🌱 - reminder of hope, warmth, thoughts and feelings (or just quotes).
LuaPass - offline password manager
By Desmond Lua
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn