实现分页组件要分三个部分

  样式,逻辑,和引用

  首先新建一个vue文件用来承载组件内容

  第一步:构建样式 

  <template>
    <nav>
    <ul class="pagination">
    <li :class="{'disabled': current == 1}"><a href="javascript:;" @click="setCurrent(current - 1)"> <img src="../../../assets/btnleft.png" alt=""> </a></li>
    <li v-for="(p,index) in grouplist" :class="{'active': current == p.val}" :key="index"><a href="javascript:;" @click="setCurrent(p.val)"> {{ p.text }} </a>
    </li>
    <li :class="{'disabled': current == page}"><a href="javascript:;" @click="setCurrent(current + 1)"> <img src="../../../assets/btnright.png" alt=""></a></li>
    </ul>
    </nav>
  </template> 

  <style scope="scope">
    .pagination {
      overflow: hidden;
      display: table;
      margin: 0 auto;
      height: 50px;
    }
    .pagination li {
      float: left;
      height: 30px;
      border-radius: 5px;
      margin: 3px;
    }
    .pagination li img{
      width:100%;
      height:100%;
     }
    .pagination li:hover{
      background: #1E7FED;
    }
    .pagination li:hover a{
      color: #fff;
    }
    .pagination a {
      display: block;
      width: 30px;
      height: 30px;
      text-align: center;
      line-height: 30px;
      font-size: 12px;
      border-radius: 5px;
      text-decoration: none;
      border:1px solid rgba(221,221,221,1);
      color: #666666;
    }
    .pagination .active {
      background: #1E7FED !important;
    }
    .active >a{
      color:white;
    }
  </style>

第二步:编写逻辑  

<script>
export default{
data(){
return {
current: this.currentPage
}
},
props: {
total: {// 数据总条数
type: Number,
default: 0
},
display: {// 每页显示条数
type: Number,
default: 20
},
currentPage: {// 当前页码
type: Number,
default: 1
},
pagegroup: {// 分页条数
type: Number,
default: 20,
coerce: function (v) {
v = v > 0 ? v : 20;
return v % 2 === 1 ? v : v + 1;
}
}
},
computed: {
page: function () { // 总页数
return Math.ceil(this.total / this.display);
},
grouplist: function () { // 获取分页页码
var len = this.page, temp = [], list = [], count = Math.floor(this.pagegroup / 2), center = this.current;
if (len <= this.pagegroup) {
while (len--) {
temp.push({text: this.page - len, val: this.page - len});
}
return temp;
}
while (len--) {
temp.push(this.page - len);
}
var idx = temp.indexOf(center);
(idx < count) && ( center = center + count - idx);
(this.current > this.page - count) && ( center = this.page - count);
temp = temp.splice(center - count - 1, this.pagegroup);
do {
var t = temp.shift();
list.push({
text: t,
val: t
});
} while (temp.length);
if (this.page > this.pagegroup) {
(this.current > count + 1) && list.unshift({text: '...', val: list[0].val - 1});
(this.current < this.page - count) && list.push({text: '...', val: list[list.length - 1].val + 1});
}
return list;
}
},
methods: {
setCurrent: function (idx) {
if (this.current != idx && idx > 0 && idx < this.page + 1) {
this.current = idx;
this.$emit('pagechange', this.current);
}
}
}
}
</script>

第三步:引用组件

  1.在父组件中引入并注册

components: {
   "v-pagination": Paging,
},
2.在data下声明三个变量

total:0, // 记录总条数

display: 10, // 每页显示条数

current: 1, // 当前的页数

3.挂载

<v-pagination v-if="totals > 0" :total="totals" :current-page='current' @pagechange="pagechange"></v-pagination>
4.添加事件
pagechange:function(currentPage){
    // console.log(currentPage);
  //该参数就是当前点击的页码数
}

Vue 实现一个分页组件的更多相关文章

  1. 基于vue2.0的一个分页组件

    分页组件在项目中经常要用到之前一直都是在网上找些jq的控件来用(逃..),最近几个项目用上vue了项目又刚好需要一个分页的功能.于是百度发现几篇文章介绍的实在方式有点复杂, 没耐心看自己动手造轮子写了 ...

  2. 使用vue完成一个分页效果

    基于 element-ui 分页组件实现分页效果 效果如下: 使用说明: 0.首先在头部引入需要的外部文件 1.从element官方网页中复制想要的组件代码直接放入body中 2.编写逻辑代码 3.完 ...

  3. vue中使用分页组件、将从数据库中查询出来的数据分页展示(前后端分离SpringBoot+Vue)

    文章目录 1.看实现的效果 2.前端vue页面核心代码 2.1. 表格代码(表格样式可以去elementui组件库直接调用相应的) 2.2.分页组件代码 2.3 .script中的代码 3.后端核心代 ...

  4. 使用Vue实现一个树组件

    HTML代码: <!DOCTYPE html> <html> <head> <title>Vue Demo</title> <meta ...

  5. 基于Vue封装分页组件

    使用Vue做双向绑定的时候,可能经常会用到分页功能 接下来我们来封装一个分页组件 先定义样式文件 pagination.css ul, li { margin: 0px; padding: 0px;} ...

  6. asp.mvc中的vue分页实例,分页组件无法重置reload,解决点击查询按钮后,分页不刷新的问题

    刚刚接触Vue.js,现在需要做一个查询功能,并且进行服务端分页.主要思路是在页面中注册一个分页组件,然后进行调用.代码如下 1.引用vue.js,具体去网上下载 2.在html的body中添加如下代 ...

  7. 基于 bootstrap 的 vue 分页组件

    申手党点这里下载示例 基于 bootstrap 的 vue 分页组件,我想会有那么一部分同学,在使用Vue的时候不使用单文件组件,因为不架设 NodeJS 服务端.那么网上流传的 *.vue 的各种分 ...

  8. Vue 2.0 pagination分页组件

    最近写了一个分页组件,效果如下图: f-pagination.js文件 Vue.component('f-pagination',{ template:'<div class="fPa ...

  9. 基于Vue的简单通用分页组件

    分页组件是每一个系统里必不可少的一个组件,分页组件分为两部分.第一部分是模版部分,用于显示当前分页组件的状态,例如正在获取数据.没有数据.没有下一页等等:第二部分是分页数据对象,用于封装一个分页组件的 ...

随机推荐

  1. 组件的属性props

    组件的属性props 是不能自己改的,一般是从外面传进来.在组件的视图中用,this.props.XXX 表示该组件的XXX 属性

  2. php实现大文件上传带进度条

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  3. [CF1188B]Count Pairs 题解

    前言 这道题目是道好题. 第一次div-2进前100,我太弱了. 题解 公式推导 我们观察这个式子. \[(a_i+a_j)(a_i^2+a_j^2)\equiv k \mod p\] 感觉少了点什么 ...

  4. Ubuntu 16.04下使用docker部署rabbitmq

    (以下docker相关的命令,需要在root用户环境下或通过sudo提升权限来进行操作.) 1.拉取rabbimq镜像到本地 docker pull rabbitmq 2. Docker运行rabbi ...

  5. Linux 安装R包

    https://www.cnblogs.com/jessepeng/p/10984983.html Linux 的R环境,可以通过anaconda jupyter notbook很容易的配置,见我之前 ...

  6. RAM 大全-DRAM, SRAM, SDRAM的关系与区别

    ROM和RAM指的都是半导体存储器,ROM是Read Only Memory的缩写,RAM是Random Access Memory的缩写.ROM在系统停止供电的时候仍然可以保持数据,而RAM通常都是 ...

  7. 6.10&&6.12考试反思

    考试结果:6.10AK 6.12:100(评测机)200(本地&&兼容评测机版) OI的考试做题流程无非是: 通读全部题目——>找一个最有把握/最简单的题——>分析思考—— ...

  8. SpringMVC开发中遇到的异常1:No primary or default constructor found for interface java.util.List

    Request processing failed; nested exception is java.lang.IllegalStateException: No primary or defaul ...

  9. Linux超全实用指令大全

    参考 Linux超全实用指令大全

  10. CMD模块打包部署总结

    目前线上系统利用Seajs做模板化,但是没有对js和css进行打包,在这次简历搜索优化项目里我尝试对gulp插件对Seajs模块打包. 安装gulp和相关插件 npm install -g gulp ...