欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue实现web分页组件详解

程序员文章站 2022-06-25 13:22:47
本文实例为大家分享了vue实现web分页组件的具体代码,供大家参考,具体内容如下 效果演示 源代码 <...

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

效果演示

Vue实现web分页组件详解

源代码

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>测试分页 - www.maoyupeng.com</title>
 <style type="text/css">
  body{padding:0; margin: 0; broder:none; } #app {width: 500px; height: 200px; margin: 0 auto; text-align: center; background-color: #cccccc; } #mylink {color: #efefef; } .pagination{list-style: none; text-align: center; height: 50px; padding-top: 50px; } .pagination > li {float: left; margin: 0 5px; } [v-cloak] {display: none; } </style>
</head>
<body>
 <div id="app" v-cloak>
  <ul class="pagination">
   <li>
    <a v-if="currentpage == 1" >首页</a>
    <a v-else href="javascript:;" @click="next(1)">首页</a>
   </li>
   <li v-if="currentpage<=1"><a>上一页</a></li>
   <li v-else><a href="javascript:;" @click="next(currentpage-1)">上一页</a></li>


   <li v-for="item in paginglist">
    <a v-if="currentpage==item.key || sign ==item.key" >{{item.key}}</a>
    <a v-else href="javascript:;" @click="next(item.value)">{{item.key}}</a>
   </li>

   <li v-if="currentpage>=totalpagecount"><a>下一页</a></li>
   <li v-else><a href="javascript:;" @click="next(currentpage+1)">下一页</a></li>
   <li>
    <a v-if="totalpagecount == currentpage">尾页</a>
    <a v-else href="javascript:;" @click="next(totalpagecount)">尾页</a>
   </li>
  </ul>
  <p>共:{{totalpagecount||0}}页,当前页为第{{currentpage||0}}页   设置总页数:<input style="width:20px;" v-model="totalpagecount"></p>
  <a href="http://www.maoyupeng.com/web-pagination-component-for-vue.html" target="_blank" id="mylink">http://www.maoyupeng.com 带注解版本</a>
 </div>

 <script src="https://unpkg.com/vue/dist/vue.js"></script>
 <script type="text/javascript">
  var app = new vue({
   el: '#app',
   data: {
   // 省略的符号
   sign:'...',
   // 省略号位置
   signindex:4,
   // 总页数
   totalpagecount: 4,
   // 当前页
   currentpage:1,
   // 显示在页面的数组列表
   paginglist:[]
   },
   watch: {
   totalpagecount (val) {
    var that = this
    if (!val || val == '') return;
    that.currentpage = 1;
    that.init()
   },
   currentpage (val) {
    var that = this
    that.init()
   }
   },
   methods: {
   // 跳转到某页码
   next (num) {
    var that = this
    if (num <= 1) that.currentpage = 1;
    else if (num >= that.totalpagecount) that.currentpage = that.totalpagecount;
    else that.currentpage = num;
   },
   // 初始化数据
   fetchdata () {
    var that = this

    that.paginglist = [];
    var tmp = null;


    if ((that.totalpagecount) > 6) {
     if (((that.totalpagecount-1) == (that.totalpagecount - that.currentpage)) && (that.totalpagecount - that.currentpage) > 5) {
      for (var i=1;i<7;i++) {
       if (i < that.signindex) {
        tmp = {key:i, value:i }
       } else if (i== that.signindex) {
        tmp = {key:that.sign, value:0 }
       } else if (i == (that.signindex + 1) ) {
        tmp = {key:that.totalpagecount - 1, value:that.totalpagecount - 1 }
       } else {
        tmp = {key:that.totalpagecount, value:that.totalpagecount }
       }
       that.paginglist.push(tmp)
      }
     } else if (((that.totalpagecount - that.currentpage) <= that.signindex)){
      var starnum = that.totalpagecount - 5;
      for (var i=starnum;i<starnum+6;i++) {
       tmp = {key:i, value:i }
       that.paginglist.push(tmp)
      }
     } else {
      var starnum = that.currentpage - 1;
      for (var i=1;i<7;i++) {
       if (i < that.signindex) {
        tmp = {key:(starnum - 1) + i, value:(starnum - 1) + i }
       } else if (i== that.signindex) {
        tmp = {key:that.sign, value:0 }
       } else if (i == (that.signindex + 1) ) {
        tmp = {key:that.totalpagecount - 1, value:that.totalpagecount - 1 }
       } else {
        tmp = {key:that.totalpagecount, value:that.totalpagecount }
       }
       that.paginglist.push(tmp)
      }
     }
    } else {
     for (var i =0; i <that.totalpagecount; i++) {
      tmp = {key:i+1, value:i+1 }
      that.paginglist.push(tmp)
     }
    }
   },
   init () {
    var that = this

    that.fetchdata()
   }
   },
   mounted () {
   var that = this

   that.init()
   }
  }) 
 </script>
</body>
</html>

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