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

JavaScript

达蒙樱梅

2020-03-12

我有这样的代码:

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” ??,谢谢再次寻求任何帮助。

第1147篇《VueJs如何使用限制器和范围进行分页?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
Tom凯飞云 2020.03.12

由于此过滤器迁移https://vuejs.org/v2/guide/migration.html#Filters-Outside-Text-Interpolations-removed,因此我分叉了@Jeff的代码,并为Vue 2创建了工作版本

分页方法移至计算:

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/

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android