首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >VueJs如何使用限制器和范围进行分页..

VueJs如何使用限制器和范围进行分页..
EN

Stack Overflow用户
提问于 2016-02-24 16:00:30
回答 2查看 27.5K关注 0票数 24

我的代码是这样的:

代码语言:javascript
复制
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);
    }
  }
});
代码语言:javascript
复制
a {
  color: #999;
}
.current {
  color: red;
}
ul {
  padding: 0;
  list-style-type: none;
}
li {
  display: inline;
  margin: 5px 5px;
}
代码语言:javascript
复制
<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”??,再次感谢任何帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-01 02:32:51

看看这段代码:

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

html:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
a.first::after {
  content:'...'
}

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

基本上,它只显示当前页面2页以内的分页。然后,它还将显示第1页和最后一页,并使用CSS将"..."置于数字之前或之后。因此,如果你在第10页,它将显示:

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

票数 37
EN

Stack Overflow用户

发布于 2018-01-21 16:21:00

由于这个过滤器迁移https://vuejs.org/v2/guide/migration.html#Filters-Outside-Text-Interpolations-removed,我已经派生了@ make的代码,并为Vue 2制作了一个工作版本。

将分页方法移至computed:

代码语言:javascript
复制
paginate: function() {
        if (!this.users || this.users.length != this.users.length) {
            return
        }
        this.resultCount = this.users.length
        if (this.currentPage >= this.totalPages) {
          this.currentPage = this.totalPages
        }
        var index = this.currentPage * this.itemsPerPage - this.itemsPerPage
        return this.users.slice(index, index + this.itemsPerPage)
    }

警告:我没有应用文本过滤器,只是先应用了分页。

https://jsfiddle.net/c1ghkw2p/

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35596389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档