登录  /  注册
首页 > web前端 > js教程 > 正文

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

巴扎黑
发布: 2017-05-14 14:27:17
原创
1359人浏览过

这篇文章主要为大家详细介绍了基于vue2的table分页组件实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

pagination.js:


(function(){
 var template = &#39;<p class="page-bar" > \
      <p class="info">{{info}}</p>\
      <p class="showpages">每页<select class="showpages-select" v-on:change="pageschange" v-model="selected" ><option v-for="item in showpages">{{item}}</option></select>条</p>\
      <p 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></p> \
     </p>\
     &#39;
 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(&#39;mypage&#39;, this.cur);
   },
   pageschange:function () {
    this.$emit(&#39;pageschange&#39;, 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=&#39;../img/common_edit@25.png&#39; data-toggle="modal"
   data-target="#editSonModal" @click="editSonModal(item,nn)" alt=&#39;修改&#39;>
  <img src=&#39;../img/common_del@25.png&#39; data-toggle="modal"
   data-target="#delSonModal" @click="delSonModal(nn)" alt=&#39;删除&#39;>
  </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: {
  &#39;vue-pagination&#39;: 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)
  }
 }
});
登录后复制

以上就是基于vue2的table分页组件实现方法的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号