时间:2020-10-08 13:27:15 | 栏目:vue | 点击:次
当前组件依赖bootstrap样式,使用前请先引用相关css。
Vue.component('pagination', { template: `<nav aria-label="Page navigation"> <ul class="pagination"> <li :class="{'disabled': pageNum == 1}"><a href="#" v-on:click.prevent="turnToPage(1)" title="首页" aria-label="首页"><i class="fa fa-fast-backward"></i></a></li> <li :class="{'disabled': pageNum == 1}"><a href="#" v-on:click.prevent="turnToPage(pageNum - 1)" title="上一页" aria-label="上一页"><i class="fa fa-backward"></i></a></li> <li v-if="pageNum > 4"><a href="#" v-on:click.prevent="turnToPage(pageNum - 4)">...</a></li> <li v-if="pageNum <= 4 && pageNum >1"><a href="#" v-on:click.prevent="turnToPage(1)">1</a></li> <li v-if="pageNum - 3 > 1"><a href="#" v-on:click.prevent="turnToPage(pageNum - 3)">{{pageNum-3}}</a></li> <li v-if="pageNum - 2 > 1"><a href="#" v-on:click.prevent="turnToPage(pageNum - 2)">{{pageNum-2}}</a></li> <li v-if="pageNum - 1 > 1"><a href="#" v-on:click.prevent="turnToPage(pageNum - 1)">{{pageNum-1}}</a></li> <li class="active"><a href="#" v-on:click.prevent="turnToPage(pageNum)">{{pageNum}}</a></li> <li v-if="pageNum + 1 < pageTotal"><a href="#" v-on:click.prevent="turnToPage(pageNum + 1)">{{pageNum+1}}</a></li> <li v-if="pageNum + 2 < pageTotal"><a href="#" v-on:click.prevent="turnToPage(pageNum + 2)">{{pageNum+2}}</a></li> <li v-if="pageNum + 3 < pageTotal"><a href="#" v-on:click.prevent="turnToPage(pageNum + 3)">{{pageNum+3}}</a></li> <li v-if="pageNum >= pageTotal - 4 && pageNum < pageTotal"><a href="#" v-on:click.prevent="turnToPage(pageTotal)">{{pageTotal}}</a></li> <li v-if="pageNum < pageTotal - 4"><a href="#" v-on:click.prevent="turnToPage(pageNum + 4)">...</a></li> <li :class="{'disabled': pageNum == pageTotal}"><a href="#" v-on:click.prevent="turnToPage(pageNum + 1)" title="下一页" aria-label="下一页"><i class="fa fa-forward"></i></a></li> <li :class="{'disabled': pageNum == pageTotal}"><a href="#" v-on:click.prevent="turnToPage(pageTotal)" title="尾页" aria-label="尾页"><i class="fa fa-fast-forward"></i></a></li> </ul> </nav>`, props: { pageNum: Number, pageSize: Number, totalItemCount: Number, }, computed: { pageTotal: function () { return Math.ceil(this.totalItemCount / this.pageSize) } }, methods: { turnToPage: function (num) { if (num > this.pageTotal || num <= 0) { //toastr.error(`当前页码超出了范围。页码:${num}`, '错误') return false } this.$emit('change', num) } } })
props定义三个属性:当前页码,当前页显示数量,总数量
computed定义了一个计算方法:获取总数量/当前页显示数量,向上取整,默认取10个
methods定义了一个根据页码跳转方法:最终用于触发change事件,$emit用于抛出自定义事件,组件外可以捕获当前定义的change事件
html组件显示:
let vm = new Vue({ el: '#app', data: { criteria: { keyword: '', page: { num: 1, size: 10 } }, itemsCount: 0 }, methods: { skiptoPage: function (num) { this.criteria.page.num = num; } } });
以上方法是自定义事件change的方法,你们可以自己去修改内容。
效果图