vue怎么自定义翻页组件
本文讲解"vue如何自定义翻页组件",希望能够解决相关问题。
效果图如下:
1、在components建立page.vue文件
<template> <div class="pagination"> <!-- 上 --> <button :disabled="pageNo == 1" @click="getPageNo(pageNo - 1)"> 上一张 </button> <button v-if="startNumAndEndNum.start > 1" @click="getPageNo(1)" :class="{ active: pageNo == 1 }" >1 </button> <button v-if="startNumAndEndNum.start > 2" @click="getPageNo(pageNo - continues)" >··· </button> <!-- 中间部分 --> <button v-for="(page, index) in generateMiddlePage" :key="index" @click="getPageNo(page)" :class="{ active: pageNo == page }"> {{ page }} </button> <!-- 下 --> <button v-if="startNumAndEndNum.end < totalPage - 1" @click="getPageNo(pageNo +continues)" >··· </button> <button v-if="startNumAndEndNum.end < totalPage" @click="getPageNo(totalPage)" :class="{active:pageNo==totalPage}"> {{ totalPage }} </button> <button :disabled="pageNo == totalPage" @click="getPageNo(pageNo + 1)"> 下一张 </button> </div> </template> <script> export default { name: "Pagination", props: ["pageNo", "pageSize", "total", "continues"], computed: { //计算出总页数 totalPage() { //向上取整 return Math.ceil(this.total / this.pageSize); }, //计算出页码的起始和结束数字 startNumAndEndNum() { const {continues, pageNo, totalPage} = this; //先定义两个变量存储起始数字与结束数字 let start = 0, end = 0; if (continues > totalPage) { start = 1; end = totalPage; } else { //起始数字 start = pageNo - parseInt(continues / 2); //结束数字 end = pageNo + parseInt(continues / 2); if (start < 1) { start = 1; end = continues; } if (end > totalPage) { end = totalPage; start = totalPage - continues + 1; } } return {start, end}; }, //过滤掉小于起始页的页码 generateMiddlePage() { let arr = []; //避免页面中同时使用 v-for和v-if for (let i = 0; i <= this.startNumAndEndNum.end; i++) { arr.push(i) } let temp = arr.filter(item => { return item >= this.startNumAndEndNum.start }) return temp } }, methods: { getPageNo(val) { //自定义事件子页面向父页面传参,计算属性值 this.$emit('getPageNo', val) }, } }; </script> <style lang="scss" scoped> .pagination { text-align: center; width: 70px; button { margin:12px 0px 0px 0px; background-color: #fff; color: #409eff; border:1px solid #409eff; outline: none; border-radius: 2px; padding: 0 4px; vertical-align: top; display: inline-block; font-size: 14px; width: 60px; height: 38px; line-height: 38px; cursor: pointer; box-sizing: border-box; text-align: center; &[disabled] { color: #c0c4cc; cursor: not-allowed; border:1px solid #ddd; } &:hover{ background: #ddeffb; } &[disabled]:hover{ background: none;} &.active { cursor: not-allowed; background-color: #409eff; color: #fff; } } } </style>
2、在页面中引入组件
<page :pageNo="pageNo" :pageSize="pageSize" :total="pageTotal" :continues="5" //超过5时中建按钮有省略号 @getPageNo="getPageNo" /> import page from '@/components/page' data(){ return{ pageNo:1, pageSize:1, pageTotal:5, } } //方法中 methods:{ getPageNo(pageNo){ this.pageNo=pageNo }, }
关于 "vue如何自定义翻页组件" 就介绍到此。希望多多支持编程宝库。
vue表单验证rules及validator验证器如何使用:本文讲解"vue表单验证rules及validator验证器怎么使用",希望能够解决相关问题。前言为防止用户犯错,尽可能更早地发现并纠正错误。Element中Form (表单)组件 ...