Vue分页器组件使用方法详解

本文实例为大家分享了Vue分页器组件的使用,供大家参考,具体内容如下

效果图如下:

鼠标悬浮时切换为箭头:

①创建自定义分页组件Pager.vue:预设主题色为@themeColor: #D93844; 每页展示10条数据,一次最多显示5个页码,支持输入页码跳转:

<template>
  <div class="m-pager-wrap" v-if="totalCount">
    <span class="u-text">共{{ totalPage }}页 / {{ totalCount }}条</span>
    <span class="u-item txt" :class="{'disabled': currentPage===1}" @click="changePage(1)">首页</span>
    <span class="u-item txt" :class="{'disabled': currentPage===1}" @click="changePage(currentPage - 1)">上一页</span>
    <span
      class="u-ellipsis"
      ref="forward"
      v-show="forwardMore"
      @click="onForward"
      @mouseenter="onEnterForward"
      @mouseleave="onLeaveForward">&middot;&middot;&middot;</span>
    <span :class="['u-item', {'active': currentPage===num}]" v-for="num in pageList" :key="num" @click="changePage(num)">{{ num }}</span>
    <span
      class="u-ellipsis"
      ref="backward"
      v-show="backwardMore"
      @click="onBackward"
      @mouseenter="onEnterBackward"
      @mouseleave="onLeaveBackward">&middot;&middot;&middot;</span>
    <span class="u-item txt" :class="{'disabled': currentPage===totalPage}" @click="changePage(currentPage + 1)">下一页</span>
    <span class="u-item txt" :class="{'disabled': currentPage===totalPage}" @click="changePage(totalPage)">尾页</span>
    <span class="u-jump-page">跳至<input type="text" v-model="jumpNumber"/>页</span>
    <span class="u-item txt" @click="jumpPage(jumpNumber)">确定</span>
  </div>
</template>
<script>
export default {
  name: 'Pager',
  data () {
    return {
      currentPage: this.pageNumber, // 当前页码
      currentSize: this.pageSize, // 分页数
      jumpNumber: '', // 跳转的页码
      forwardMore: false, // 左箭头展示
      backwardMore: false // 右箭头展示
    }
  },
  props: {
    pageNumber: { // 当前页面
      type: Number,
      default: 1
    },
    pageSize: { // 每页显示数量 [10条/页 20条/页 30条/页 40条/页]
      type: Number,
      default: 10
    },
    totalCount: { // 总条数
      type: Number,
      default: 0
    },
    pageListNum: { // 显示的页码数组长度
      type: Number,
      default: 5
    }
  },
  computed: {
    totalPage () { // 总页数
      return Math.ceil(this.totalCount / this.currentSize) // 向上取整
    },
    pageList () { // 获取显示的页码数组
      return this.dealPageList(this.currentPage)
    }
  },
  watch: {
    currentPage (to, from) {
      // 通过更改当前页码,修改分页数据
      this.$emit('changePage', { currentPage: to, currentSize: this.currentSize })
    }
  },
  created () {
    // 监听键盘Enter按键
    document.onkeydown = (e) => {
      const ev = e || window.event
      if (ev && ev.keyCode === 13 && this.jumpNumber) {
        this.jumpPage(this.jumpNumber)
      }
    }
  },
  methods: {
    dealPageList (curPage) {
      var resList = []
      var offset = Math.floor(this.pageListNum / 2) // 向下取整
      var pager = {
        start: curPage - offset,
        end: curPage + offset
      }
      if (pager.start < 1) {
        pager.end = pager.end + (1 - pager.start)
        pager.start = 1
      }
      if (pager.end > this.totalPage) {
        pager.start = pager.start - (pager.end - this.totalPage)
        pager.end = this.totalPage
      }
      if (pager.start < 1) {
        pager.start = 1
      }
      if (pager.start > 1) {
        this.forwardMore = true
      } else {
        this.forwardMore = false
      }
      if (pager.end < this.totalPage) {
        this.backwardMore = true
      } else {
        this.backwardMore = false
      }
      // 生成要显示的页码数组
      for (let i = pager.start; i <= pager.end; i++) {
        resList.push(i)
      }
      return resList
    },
    onEnterForward () {
      this.$refs.forward.innerHTML = '&laquo;'
    },
    onLeaveForward () {
      this.$refs.forward.innerHTML = '&middot;&middot;&middot;'
    },
    onEnterBackward () {
      this.$refs.backward.innerHTML = '&raquo;'
    },
    onLeaveBackward () {
      this.$refs.backward.innerHTML = '&middot;&middot;&middot;'
    },
    onForward () {
      this.currentPage = this.currentPage - this.pageListNum > 0 ? this.currentPage - this.pageListNum : 1
    },
    onBackward () {
      this.currentPage = this.currentPage + this.pageListNum < this.totalPage ? this.currentPage + this.pageListNum : this.totalPage
    },
    jumpPage (jumpNum) {
      if (Number(jumpNum)) {
        if (Number(jumpNum) < 1) {
          jumpNum = 1
        }
        if (Number(jumpNum) > this.totalPage) {
          jumpNum = this.totalPage
        }
        this.changePage(Number(jumpNum))
      }
      this.jumpNumber = '' // 清空跳转输入框
    },
    changePage (pageNum) {
      if (this.currentPage !== pageNum) { // 点击的页码不是当前页码
        this.currentPage = pageNum
      }
    }
  }
}
</script>
<style lang="less" scoped>
@themeColor: #D93844; // 自定义主题色
.m-pager-wrap {
  height: 32px;
  line-height: 30px;
  font-size: 14px;
  color: #888;
  text-align: center;
  .u-text {
    margin-right: 5px;
  }
  .u-item {
    margin-right: 5px;
    min-width: 30px;
    display: inline-block;
    user-select: none; // 禁止选取文本
    border: 1px solid #d9d9d9;
    border-radius: 4px;
    cursor: pointer;
    &:hover {
      .active();
    }
  }
  .active {
    color: #fff;
    background: @themeColor;
    border: 1px solid @themeColor;
  }
  .disabled {
    color: rgba(0,0,0,.25);
    &:hover {
      cursor: not-allowed;
      color: rgba(0,0,0,.25);
      background: #fff;
      border: 1px solid #d9d9d9;
    }
  }
  .txt {
    padding: 0 6px;
  }
  .u-ellipsis {
    display: inline-block;
    width: 12px;
    padding: 0 8px;
    margin-right: 5px;
    cursor: pointer;
    &:hover {
      color: @themeColor;
    }
  }
  .u-jump-page {
    margin: 0 8px 0 3px;
    input {
      width: 32px;
      height: 20px;
      padding: 5px 8px;
      margin: 0 5px;
      border: 1px solid #d9d9d9;
      border-radius: 4px;
      text-align: center;
    }
  }
}
</style>

②在要使用的页面引入分页器:

<div class="m-page">
    <Pager
        @changePage="jumpPage"
        :totalCount="totalCount"
        :pageNumber="queryParams.p"
        :pageSize="queryParams.pageSize" />
</div>
 
import Pager from '@/components/Pager'
components: {
    Pager
}
totalCount: 0,
queryParams: {
        pageSize: 10,
        p: 1,
        mod: 'search'
},
 
jumpPage (e) {
      this.queryParams.p = e.currentPage
      this.queryParams.pageSize = e.currentSize
      this.getDataLists() // 调用接口获取列表数据
}
 
.m-page {
  margin: 30px auto 60px;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • vuejs2.0实现一个简单的分页示例

    最近几个项目用上vue了项目又刚好需要一个分页的功能.于是百度发现几篇文章介绍的实在方式有点复杂,没耐心看自己动手写了一个. 用js实现的分页结果如图所示: css .page-bar{ margin:40px; } ul,li{ margin: 0px; padding: 0px; } li{ list-style: none } .page-bar li:first-child>a { margin-left: 0px } .page-bar a{ border: 1px solid #dd

  • 用Vue写一个分页器的示例代码

    之前一直想要自己试着实现一个分页器,但是一直拖,今天写完,大概照着网易云音乐的样子来完成.这个小例子很简单,通过这个小例子,可以学习到Vue计算属性的使用,并了解到写分页器需要区分的情况.这篇文章会慢慢从头来实现这个小例子,相信你一定会学会,而且看完了我的思路之后说不定会有更棒的思路和想法! 实现的效果是这样子的: 一.先简单布局 <template> <div class="pageContainer"> <ul class="pagesInn

  • Vue.js实现无限加载与分页功能开发

    本篇文章是一篇Vue.js的教程,目标在于用一种常见的业务场景--分页/无限加载,帮助读者更好的理解Vue.js中的一些设计思想.与许多Todo List类的入门教程相比,更全面的展示使用Vue.js完成一个需求的思考过程:与一些构建大型应用的高阶教程相比,又更专注于一些零碎细节的实现,方便读者快速掌握.致用. 需求分析 当一个页面中信息量过大时(例如一个新闻列表中有200条新闻需要展示),就会产生问题,例如: >数据量过大,影响加载速度 >用户体验差,很难定位到之前自己看过的某篇文章 >

  • Vue2.5 结合 Element UI 之 Table 和 Pagination 组件实现分页功能

    2017年底了,总结了这一年多来的前端之路,Vue从入门到放弃,再二进宫,从 Vue1.0 持续跟踪到 Vue2.5.结合公司的一些实际项目,也封装了一些比较实用的组件. 由于现在公司管理平台主要运用Element UI,索性就结合组件Table 和 Pagination 封装了一个支持页面切换的Table组件,不啰嗦,直接上代码. 2.实现思路 2.1.Element UI 引入(整体引入) main.js // Element UI import Element from 'element-

  • Vue.js实现多条件筛选、搜索、排序及分页的表格功能

    与上篇实践教程一样,在这篇文章中,我将继续从一种常见的功能--表格入手,展示Vue.js中的一些优雅特性.同时也将对filter功能与computed属性进行对比,说明各自的适用场景,也为vue2.0版本中即将删除的部分filter功能做准备. 需求分析 还是先从需求入手,想想实现这样一个功能需要注意什么.大致流程如何.有哪些应用场景. 表格本身是一种非常常用的组件,用于展示一些复杂的数据时表现很好. 当数据比较多时,我们需要提供一些筛选条件,让用户更快列出他们关注的数据. 除了预设的一些筛选条

  • 利用vue + element实现表格分页和前端搜索的方法

    前言 ElementUI是饿了么前端开源的一个基于Vue的前端框架,已经帮我们封装好了一系列功能性的组件,比如栅格系统.表格.表单.树形菜单.通知等.对于搞后台管理界面的项目,特别是不需要考虑兼容ie8.ie9以下的项目.ElementUI是一个不错的选择. 而且ElementUI的文档写得十分详尽,参照demo可以很快上手. 本文主要介绍了关于vue + element实现表格分页和前端搜索的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 实现思路 1.前端后台管理

  • Vue+element-ui 实现表格的分页功能示例

    本文介绍了Vue+element-ui 实现表格的分页功能示例,分享给大家,具体如下: 实现效果如下图所示: template部分: <el-table :data="tempList" :header-cell-style="rowClass" stripe border style="margin-bottom:14px;" :empty-text="emptyText"> <el-table-colum

  • Vue.js实现分页查询功能

    本文实例为大家分享了Vue.js实现分页查询的具体代码,供大家参考,具体内容如下 vue.js的使用如下: 1.引入vue.js <script src="~/js/vue2.2.4.js"></script> a.分页条 <ul class="pagination" id="pagination1"></ul> b.分页条js.css <link href="~/css/page.

  • Vue form 表单提交+ajax异步请求+分页效果

    废话不多说了,直接给大家贴代码了,具体代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <meta charset="UTF-

  • 基于Vue.js的表格分页组件

    一.Vue.js简介 1.Vue的主要特点: (1) 简洁 (2) 轻量 (3)快速 (4) 数据驱动 (5) 模块友好 (6) 组件化 (1) 简洁 下面看一段Angular的实现双向绑定的代码 // html <body ng-app="myApp"> <div ng-controller="myCtrl"> <p>{{ note }}</p> <input type="text" ng-

随机推荐