【问题标题】:VueJs how to make pagination with limiter and range..?VueJs如何使用限制器和范围进行分页..?
【发布时间】:2016-06-06 09:39:52
【问题描述】:

我有这样的代码:

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" ? ?,再次感谢您的帮助。

【问题讨论】:

  • 你能进一步解释你想要的输出是什么吗?
  • 我想要这样的分页号“1-2-3-4-5 ... last”而不是全部 1-2-3-4-5-6 -7-8
  • 你可以做一个 ngif="$index - page

标签: javascript jquery laravel-4 pagination vue.js


【解决方案1】:

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

paginate 方法被移动到计算:

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/

【讨论】:

    【解决方案2】:

    查看此代码:

    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 页,它将显示:

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

    【讨论】:

    • 我在示例中尝试了此代码,但由于某种原因它给了我一个警告:[Vue 警告]:属性或方法“分页”未在实例上定义,但在渲染期间被引用。确保在 data 选项中声明反应数据属性。 (在根实例中找到)我在过滤器中有分页功能,这是内部方法。
    猜你喜欢
    • 1970-01-01
    • 2018-05-13
    • 2011-07-28
    • 2016-07-01
    • 1970-01-01
    • 2017-10-03
    • 2017-06-03
    • 2018-10-19
    • 1970-01-01
    相关资源
    最近更新 更多