100行代码实现一个vue分页组功能

作者:伊泽瑞尔 时间:2024-05-05 09:23:23 

今天用vue来实现一个分页组件,总体来说,vue实现比较简单,样式部分模仿了elementUI。所有代码的源码可以再github上下载的到:下载地址 先来看一下实现效果:

100行代码实现一个vue分页组功能

点击查看效果

整体思路

我们先看一下使用到的文件的目录:

100行代码实现一个vue分页组功能

我们在 pageComponentsTest.vue 页面引入了 pageComponent.vue 分页组件。整体思路是通过 props
来达到组件的灵活通用的效果,整体语法是使用vue的VM语法。

pageComponent.vue实现

首先实现一个分页,需要知道数据总条数,一个页面显示的数据条数和当前显示第几页的数据。那么我们在 pageComponent.vue 里面的 props 就有了。看下面的代码:


props: {
  // 分页配置
  pageConfig: {
   type: Object, require: true, default() {
    return {
     pageSize: 10,   //一页的数据条数
     pageNo: 0,    //当前页的索引
     total: 0,     //总的数据条数
     pageTotal: 0   //总的页数
    }
   }
  }

根据用户入参,我们可以使用计算属性来计算一个总页数的变量:


computed: {
  //计算总页数,如果传了pageTotal,直接取pageTotal的值,如果传了total,那么根据pageSize去计算
  pageTotal(){
   const config = this.pageConfig
   if(config.pageTotal){
    return config.pageTotal
   }else {
    if(config.pageSize && config.total){
     return Math.ceil(config.total/config.pageSize)
    }else {
     return 0
    }
   }
  }
 }

有了总页数,和当前页,就需要各种判断来实现我们的html部分了,这里分4中情况

  1. 总页数小于8,只需要直接遍历到8就行了。

  2. 总页数大于8,但当前页小于4的。

  3. 总页数大于8,当前页靠后的。

  4. 总页数大于8,当前页在中间的。

下面看具体的实现:


<!--上一页-->
  <button @click="prePage" :disabled="currentPage === 1">上一页</button>
  <!--总页数小于8的-->
  <template v-if="pageTotal <= showPageNo">
   <button v-for="i in pageTotal" @click="changeCurrentPage(i)" :class="{active:i === currentPage}" :key="i">{{i}}</button>
  </template>
  <template v-else-if="currentPage < 4">
   <button v-for="i in 6" @click="changeCurrentPage(i)" :class="{active:i === currentPage}" :key="i">{{i}}</button>
   <button :disabled="true">···</button>
   <button>{{pageTotal}}</button>
  </template>
  <template v-else-if="currentPage > pageTotal - 4">
   <button>1</button>
   <button :disabled="true">···</button>
   <button v-for="i in 6" @click="changeCurrentPage(i + (pageTotal - 6))" :class="{active:(i + (pageTotal - 6)) === currentPage}" :key="i">{{i + (pageTotal - 6)}}</button>
  </template>
  <template v-else>
   <button>1</button>
   <button :disabled="true">···</button>
   <button @click="changeCurrentPage(currentPage - 2)">{{currentPage - 2}}</button>
   <button @click="changeCurrentPage(currentPage - 1)">{{currentPage - 1}}</button>
   <button class="active">{{currentPage}}</button>
   <button @click="changeCurrentPage(currentPage + 1)">{{currentPage + 1}}</button>
   <button @click="changeCurrentPage(currentPage + 2)">{{currentPage + 2}}</button>
   <button :disabled="true">···</button>
   <button @click="changeCurrentPage(pageTotal)">{{pageTotal}}</button>
  </template>
  <!--下一页-->
  <button @click="nextPage" :disabled="currentPage === pageTotal">下一页</button>

可以看到页面上需要实现3个方法,分别是上下页,和点击页面的方法。


methods: {
  prePage(){
   this.currentPage -= 1
   this.$emit('changeCurrentPage',this.currentPage)
  },
  nextPage(){
   this.currentPage += 1
   this.$emit('changeCurrentPage',this.currentPage)
  },
  changeCurrentPage(i){
   this.currentPage = i
   this.$emit('changeCurrentPage',this.currentPage)
  }
 }

pageComponentsTest.vue的实现

引用页面就比较简单了,只要传入组件需要的对应的参数,就能显示我们的组件了。 引用部分:


<template>
<div class="pageComponentsTest">
 <page-component :page-config="pageConfigTotal" @changeCurrentPage="changePage"></page-component>
 <page-component :page-config="pageConfigPageTotal"></page-component>
</div>
</template>

配合入参部分:


{
 name: "pageComponentsTest",
 data() {
  return {
   pageConfigTotal:{total:21,pageSize:10,pageNo:1},
   pageConfigPageTotal:{total:21,pageSize:10,pageNo:1,pageTotal:50}
  }
 },
 components:{'page-component':pageComponent},
 methods: {
  changePage(page){
   this.pageConfigTotal.pageNo = page
  }
 }
}

总结

可以看到使用vue实现分页组件整体来说是很容易了,比使用jQuery方便很多,使用vm模式开发前端的最明显的一个好处是,能是数据mode部分与view页面部分保持同步,而开发者不用考虑这个过程,所以整体来说简单了很多。

以上所述是小编给大家介绍的100行代码实现一个vue分页组功能网站的支持!

来源:https://juejin.im/post/5be02a9a6fb9a049e41226d5

标签:vue,分页,组件
0
投稿

猜你喜欢

  • Python实现优先级队列结构的方法详解

    2022-06-15 20:59:33
  • 详解python配置虚拟环境

    2021-08-02 22:02:50
  • 浅议Wap网页设计中的锚点链接

    2010-12-17 12:41:00
  • Python常用图像形态学操作详解

    2023-07-27 18:14:51
  • python爬虫面试宝典(常见问题)

    2023-02-27 19:46:01
  • SQL 查询性能优化 解决书签查找

    2012-10-07 10:23:56
  • Python中文件遍历的两种方法

    2022-01-28 20:34:03
  • 不拘小节的中文字体设计

    2009-05-21 10:44:00
  • Python networkx包的实现

    2023-07-29 23:58:03
  • SQLSERVERS 数据整理方法

    2024-01-13 11:09:44
  • SQL Server2019安装的详细步骤实战记录(亲测可用)

    2024-01-28 13:04:51
  • pandas获取groupby分组里最大值所在的行方法

    2021-08-14 21:39:14
  • python Pandas之DataFrame索引及选取数据

    2023-01-01 02:27:10
  • Golang解析yaml文件操作指南

    2024-05-09 14:51:59
  • MySql使用skip-name-resolve解决外网链接客户端过慢问题

    2024-01-26 16:07:35
  • Python 命令行解析工具 argparse基本用法

    2023-06-15 01:34:46
  • 详解python连接telnet和ssh的两种方式

    2023-02-10 01:27:14
  • python实现udp传输图片功能

    2022-09-24 16:35:01
  • python 解析XML python模块xml.dom解析xml实例代码

    2022-11-24 15:15:09
  • Sql Server 和 Access 操作数据库结构Sql语句小结

    2024-01-28 14:13:39
  • asp之家 网络编程 m.aspxhome.com