基于vue2的table分页组件实现方法


Posted in Javascript onMarch 20, 2017

本文实例为大家分享了vue2 table分页组件的具体代码,供大家参考,具体内容如下

pagination.js:

(function(){
 var template = '<div class="page-bar" > \
      <div class="info">{{info}}</div>\
      <div class="showpages">每页<select class="showpages-select" v-on:change="pageschange" v-model="selected" ><option v-for="item in showpages">{{item}}</option></select>条</div>\
      <div class="pagesbtn"><ul v-on:click="setpage"> \
      <li ><a v-bind:class="setButtonClass(0)" v-on:click="firstPage()">首页</a></li> \
      <li><a v-bind:class="setButtonClass(0)" v-on:click="prvePage()">上一页</a></li> \
      <li v-for="index in indexs" v-bind:class="{ active: cur == index }"> \
       <a v-on:click="btnclick(index)" >{{ index < 1 ? "..." : index }}</a> \
      </li> \
      <li ><a v-bind:class="setButtonClass(1)" v-on:click="nextPage()">下一页</a></li> \
      <li ><a v-bind:class="setButtonClass(1)" v-on:click="lastPage()">尾页</a></li> \
      </ul></div> \
     </div>\
     '
 var pagination = Vue.extend({
  template: template,
  props: ["cur", "all", "selected", "showpages", "info"],
  computed: {
   indexs: function () {
    var left = 1
    var right = this.all
    var ar = []
    if (this.all >= 11) {
     if (this.cur > 5 && this.cur < this.all - 4) {
      left = this.cur - 5
      right = this.cur + 4
     } else {
      if (this.cur <= 5) {
       left = 1
       right = 10
      } else {
       right = this.all
       left = this.all - 9
      }
     }
    }
    while (left <= right) {
     ar.push(left)
     left++
    }
    if (ar[0] > 1) {
     ar[0] = 1;
     ar[1] = -1;
    }
    if (ar[ar.length - 1] < this.all) {
     ar[ar.length - 1] = this.all;
     ar[ar.length - 2] = 0;
    }
    return ar
   }
  },
  methods: {
   btnclick: function (page) {
    this.cur = page;
   },
   nextPage: function () {
    if (this.cur >= this.all) {
     this.cur=this.all;
    }else{
     this.cur++;
    }
   },
   prvePage: function () {
    if (this.cur <= 1) {
      this.cur=1;
    }else{
     this.cur--;
    }
   },
   firstPage: function () {
    this.cur=1;
   },
   lastPage: function () {
    this.cur=this.all;
   },
   setButtonClass: function (isNextButton) {
    if (isNextButton) {
     return this.cur >= this.all ? "page-button-disabled" : ""
    }
    else {
     return this.cur <= 1 ? "page-button-disabled" : ""
    }
   },
   setpage:function () {
    this.$emit('mypage', this.cur);
   },
   pageschange:function () {
    this.$emit('pageschange', this.selected);
   }
  }
 })
 window.Pagination = pagination
})()

pagination.css:

ul, li {
margin: 0;
padding: 0;
}


.page-bar {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
float: right;
border-radius: 4px;
}
.page-bar .info{
float: left;
margin-left:16px;
font-size: 16px;
height: 100%;
}
.page-bar .showpages{
float: left;
font-size: 16px;
margin-left: 16px;
height: 100%;
}
.page-bar .showpages .showpages-select{
width: 70px;
margin: 0 10px;
height: 28px
}
.page-bar .pagesbtn{
float: left;
margin-left:16px;
width: 650px;
height: 100%;
}
.page-bar .pagesbtn ul{
text-align: center;
width: 100%;
}
.page-button-disabled {
color:#ddd !important;
}
.page-bar li {
list-style: none;
display: inline-block;
}


.page-bar li:first-child > a {
margin-left: 0;
}


.page-bar a {
border: 1px solid #ddd;
text-decoration: none;
position: relative;
float: left;
padding: 6px 12px;
line-height: 1.42857143;
color: #337ab7;
cursor: pointer;
}


.page-bar a:hover {
background-color: #eee;
}


.page-bar .active a {
color: #fff;
cursor: default;
background-color: #1e7aca;
border-color: #1e7aca;
}


.page-bar i {
font-style: normal;
color: #1e7aca;
margin: 0 4px;
font-size: 12px;
}

index.html:

<table class="table table-bordered table-hover "id="ggztable" v-show="isAddSpecifications">
  <thead>
  <tr>
  <th>规格值</th>
  <th>操作</th>
  </tr>
  </thead>
  <tbody>
  <tr v-for="(item,nn) in limitTemps">
  <td>{{item.value}}</td>
  <td>
  <img src='../img/common_edit@25.png' data-toggle="modal"
   data-target="#editSonModal" @click="editSonModal(item,nn)" alt='修改'>
  <img src='../img/common_del@25.png' data-toggle="modal"
   data-target="#delSonModal" @click="delSonModal(nn)" alt='删除'>
  </td>
  </tr>
  </tbody>
  </table>
<vue-pagination :cur="specificationValCur":all="specificationValAll":info="specificationValInfo" :showpages="specificationValShowpages":selected="specificationValselected"
 v-on:mypage="getPage" v-on:pageschange="getspecificationValShowPages">

</vue-pagination>

index.js

/**
 * Created by komi on 2017-03-05 0005.
 */



var vm = new Vue({
 el: ".main",
 data: {
  specificationValCur: 1,//当前页
  specificationValAll: 1,//总页数
  specificationValselected: 10,//默认每页显示的页数
  specificationValTotalRecond: 1,//总记录数
  specificationValShowpages: [10, 30, 50, 100], //每页显示的页数
  specificationValInfo: "",
  limitTemps: [],
  temps:[]//数据源
 },
 watch: {
  temps: "setPage"
 },
 components: {
  'vue-pagination': Pagination
 },
 methods: {
  setPage: function () {
   this.specificationValInfo = "记录数为:" + this.temps.length + "条";
   this.specificationValTotalRecond = this.temps.length;
   this.setPageBtn();
   this.setPageLimit(this.specificationValTotalRecond,this.specificationValselected,1)
  },
  getPage:function (msg) {
   this.specificationValCur=msg;//这里必须,否则按钮无法高亮
   this.setPageLimit(this.specificationValTotalRecond,this.specificationValselected,msg)
  },
  setPageLimit: function (total,select,cur) {//这里为实现分页切换table的主要实现
   if(total<=select){
    this.limitTemps=this.temps;
    return
   }else {
    var arr = [];
    var a=select*(cur-1);
    var b=select*cur;
    for (var i = a; i < b; i++) {
     if(typeof(this.temps[i])!="undefined"){
      arr[i - a] = this.temps[i]
     }
    }
    this.limitTemps = arr;
   }
   console.log("total:"+total+"select"+select+"cur"+cur)
  },
  setPageBtn: function () {
   if (this.specificationValTotalRecond > this.specificationValselected) {
    if (this.specificationValTotalRecond % this.specificationValselected == 0) {
     this.specificationValAll = this.specificationValTotalRecond / this.specificationValselected
    } else {
     this.specificationValAll = parseInt(this.specificationValTotalRecond / this.specificationValselected) + 1
    }
   } else {
    this.specificationValAll = 1
   }
  },
  getspecificationValShowPages: function (pages) {
   this.specificationValselected = pages;
   this.setPageBtn();
   this.setPageLimit(this.specificationValTotalRecond,this.specificationValselected,1)
  }
 }
});

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

Javascript 相关文章推荐
重写javascript中window.confirm的行为
Oct 21 Javascript
JSON相关知识汇总
Jul 03 Javascript
JS+CSS实现经典的左侧竖向滑动菜单效果
Sep 23 Javascript
基于jQuery实现交互体验社会化分享代码附源码下载
Jan 04 Javascript
JS利用cookies设置每隔24小时弹出框
Apr 20 Javascript
js获取浏览器的各种属性
Apr 27 Javascript
JQuery Ajax 异步操作之动态添加节点功能
May 24 jQuery
基于bootstrop常用类总结(推荐)
Sep 11 Javascript
使用vue + less 实现简单换肤功能的示例
Feb 21 Javascript
JavaScript设计模式之构造器模式(生成器模式)定义与用法实例分析
Jul 26 Javascript
vue-cli 3.x 配置Axios(proxyTable)跨域代理方法
Sep 19 Javascript
JS面向对象编程——ES6 中class的继承用法详解
Mar 03 Javascript
详解Weex基于Vue2.0开发模板搭建
Mar 20 #Javascript
基于vue.js实现侧边菜单栏
Mar 20 #Javascript
微信小程序 参数传递实例代码
Mar 20 #Javascript
vue.js指令v-model使用方法
Mar 20 #Javascript
微信小程序中子页面向父页面传值实例详解
Mar 20 #Javascript
JS查找英文文章中出现频率最高的单词
Mar 20 #Javascript
vue.js中指令Directives详解
Mar 20 #Javascript
You might like
PHP模板引擎SMARTY
2006/10/09 PHP
在字符串指定位置插入一段字符串的php代码
2010/02/16 PHP
一个PHP的QRcode类与大家分享
2011/11/13 PHP
为你总结一些php信息函数
2015/10/21 PHP
使用Composer安装Yii框架的方法
2016/03/15 PHP
简介PHP的Yii框架中缓存的一些高级用法
2016/03/29 PHP
js 刷新页面的代码小结 推荐
2010/04/02 Javascript
超级酷和最实用的jQuery实例收集(20个)
2010/04/21 Javascript
javascript中使用replaceAll()函数实现字符替换的方法
2010/12/25 Javascript
JS文本获得焦点清除文本文字的示例代码
2014/01/13 Javascript
直接在JS里创建JSON数据然后遍历使用
2014/07/25 Javascript
Bootstrap 填充Json数据的实例代码
2017/01/11 Javascript
原生js实现网页顶部自动下拉/收缩广告效果
2017/01/20 Javascript
JS图片预加载插件详解
2017/06/21 Javascript
vue实现单选和多选功能
2017/08/11 Javascript
详解在网页上通过JS实现文本的语音朗读
2019/03/28 Javascript
[01:07:22]2014 DOTA2华西杯精英邀请赛 5 24 DK VS VG加赛
2014/05/26 DOTA
[38:39]完美世界DOTA2联赛循环赛 IO vs GXR BO2第二场 11.04
2020/11/05 DOTA
python元组操作实例解析
2014/09/23 Python
Python中Django框架下的staticfiles使用简介
2015/05/30 Python
在python下读取并展示raw格式的图片实例
2019/01/24 Python
Python3.5常见内置方法参数用法实例详解
2019/04/29 Python
Python 的AES加密与解密实现
2019/07/09 Python
python并发编程 Process对象的其他属性方法join方法详解
2019/08/20 Python
使用python+whoosh实现全文检索
2019/12/09 Python
python实现一个简单RPC框架的示例
2020/10/28 Python
Bulk Powders意大利:运动补充在线商店
2019/02/09 全球购物
英国自行车商店:AW Cycles
2021/02/24 全球购物
2019年.net常见面试问题
2012/02/12 面试题
Java基础知识面试题
2014/03/25 面试题
文员自我评价怎么写
2013/09/19 职场文书
酒店中秋节活动方案
2014/01/31 职场文书
公司门卫岗位职责
2014/03/15 职场文书
解除劳动关系协议书范文
2014/09/11 职场文书
60句有关成长的名言
2019/09/04 职场文书
初中生入团申请书范文(五篇)
2019/10/16 职场文书