VueJs如何使用限制器和范围进行分页..? [英] VueJs how to make pagination with limiter and range..?

查看:27
本文介绍了VueJs如何使用限制器和范围进行分页..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

I have code like this :

var demoList = new Vue({
  el: '#demoList',
  data: {
    items: [{
      "id": 1,
      "name": "Tom"
    }, {
      "id": 2,
      "name": "Kate"
    }, {
      "id": 3,
      "name": "Jack"
    }, {
      "id": 4,
      "name": "Jill"
    }, {
      "id": 5,
      "name": "aa"
    }, {
      "id": 6,
      "name": "bb"
    }, {
      "id": 7,
      "name": "cc"
    }, {
      "id": 8,
      "name": "dd"
    }, {
      "id": 1,
      "name": "Tom"
    }, {
      "id": 2,
      "name": "Kate"
    }, {
      "id": 3,
      "name": "Jack"
    }, {
      "id": 4,
      "name": "Jill"
    }, {
      "id": 5,
      "name": "aa"
    }, {
      "id": 6,
      "name": "bb"
    }, {
      "id": 7,
      "name": "cc"
    }, {
      "id": 8,
      "name": "dd"
    }, ],
    loading: false,
    order: 1,
    searchText: null,
    ccn: null,
    currentPage: 0,
    itemsPerPage: 2,
    resultCount: 0
  },
  computed: {
    totalPages: function() {
      console.log(Math.ceil(this.resultCount / this.itemsPerPage) + "totalPages");
      return Math.ceil(this.resultCount / this.itemsPerPage);

    }
  },
  methods: {
    setPage: function(pageNumber) {
      this.currentPage = pageNumber;
      console.log(pageNumber);
    }
  },
  filters: {
    paginate: function(list) {
      this.resultCount = this.items.length;
      console.log(this.resultCount + " Result count");
      console.log(this.currentPage + " current page");
      console.log(this.itemsPerPage + " items per page");
      console.log(this.totalPages + " Total pages 2");
      if (this.currentPage >= this.totalPages) {
        this.currentPage = Math.max(0, this.totalPages - 1);
      }
      var index = this.currentPage * this.itemsPerPage;
      console.log(index + " index");
      console.log(this.items.slice(index, index + this.itemsPerPage));
      return this.items.slice(index, index + this.itemsPerPage);
    }
  }
});

a {
  color: #999;
}
.current {
  color: red;
}
ul {
  padding: 0;
  list-style-type: none;
}
li {
  display: inline;
  margin: 5px 5px;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.js"></script>
<div id="demoList">
  <div class="containerTable">
    <table class="table">
      <thead>
        <tr class="header">
          <th>
            <div><a @click="sortvia('provider_name')">Provider</a>
            </div>
          </th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in items | paginate">
          <td>{{item.name}}</td>
        </tr>
      </tbody>
    </table>
  </div>
  <ul>
    <li v-for="pageNumber in totalPages">
      <a href="#" @click="setPage(pageNumber)">{{ pageNumber+1 }}</a>
    </li>
  </ul>
</div>

我一直在尝试使用 vuejs 创建寻呼机,所以我想知道是否有人可以指定一个示例,说明如何在可能的情况下制作这样的寻呼机 "1-2-3-4-5 ... 55" ??,再次感谢您的帮助.

Im stuck trying to create a pager with vuejs, so I was wonder if anyone can appoint an example of how to make a pager like this if is possible "1-2-3-4-5 ... 55" ??, thanks again for any help.

推荐答案

查看这段代码:

https://jsfiddle.net/os7hp1cy/48/

html:

<ul>
    <li v-for="pageNumber in totalPages" 
        v-if="Math.abs(pageNumber - currentPage) < 3 || pageNumber == totalPages - 1 || pageNumber == 0">
    <a href="#" @click="setPage(pageNumber)"  
       :class="{current: currentPage === pageNumber, last: (pageNumber == totalPages - 1 && Math.abs(pageNumber - currentPage) > 3), first:(pageNumber == 0 && Math.abs(pageNumber - currentPage) > 3)}">
       {{ pageNumber+1 }}</a>
    </li>
</ul>

CSS:

a.first::after {
  content:'...'
}

a.last::before {
  content:'...'
}

基本上,它只显示当前页面 2 页内的分页.然后它还会显示第 1 页和最后一页,并将 "..." 放在带有 CSS 的数字之前或之后.因此,如果您在第 10 页,它将显示:

Basically, it only shows pagination that is within 2 pages of the current page. Then it will also show page 1 and the last page, and will put "..." before or after the number with CSS. So if you are on page 10, it will show:

1...8 9 10 11 12 ...21

这篇关于VueJs如何使用限制器和范围进行分页..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆