Vuetify - 如何进行分页?

Vuetify - how to make pagination?

我想在 Vuetify 框架中为 VueJS 使用分页。

来自 Vuetify 的分页组件:

<v-pagination
        v-model="pagination.page"
        :length="pagination.total / 5"
        :total-visible="pagination.visible"
></v-pagination>

我想在用户点击按钮时执行一个功能。我想获取页码,然后在参数中使用此页码执行函数。

来自方法 getItems 的代码:

this.pagination.page = response.body.page
this.pagination.total = response.body.total
this.pagination.perPage = response.body.perPage

数据:

data () {
  return {
    items: [],
    pagination: {
      page: 1,
      total: 0,
      perPage: 0,
      visible: 7
    }
  }
}

评论:
在你实现分页之前,先看看你是否真的需要它,或者你可以使用替代方法:
https://slack.engineering/evolving-api-pagination-at-slack-1c1f644f8e12
https://dzone.com/articles/why-most-programmers-get-pagination-wrong
http://allyouneedisbackend.com/blog/2017/09/24/the-sql-i-love-part-1-scanning-large-table/
https://www.xarg.org/2011/10/optimized-pagination-using-mysql/
https://www.eversql.com/faster-pagination-in-mysql-why-order-by-with-limit-and-offset-is-slow/



答案:

您可以使用 watcherpagination.page 更改做出反应,因为 pagination.page 会在按钮单击时更改,然后执行您的 method

watch: {
    "pagination.page": (newPage) => {
        this.onPageChange(newPage);
    }
}

或响应组件的 input 事件:

<v-pagination
    @input="onPageChange"
></v-pagination>

查看事件部分的文档。我找到了处理新页面的输入事件。

<v-pagination
  v-model="pagination.page"
  :length="pagination.pages"
  @input="next"
></v-pagination>

我的下一个方法:

next (page) {
  api.getBillList(page)
    .then(response => {
      this.bills = response.data.content
      console.log(this.bills)
    })
    .catch(error => {
      console.log(error)
    })
}

我在搜索尝试在我的 VueJS 项目中实现此分页时收到的错误后来到这里:[Vue warn]: Invalid prop: custom validator check failed for prop "length".

我的问题是我对长度的计算得出的是十进制答案,这看起来像是您在问题的示例代码中可能遇到的问题。例如,如果我有 23 条记录和 5 的页面大小,它将 return 4.6,给我上面的错误。我必须将我的计算包含在 Math.ceil() 中以获得适当的长度值。

希望这对某人有所帮助:)

<v-pagination
   v-model="currPage"
   :length="Math.ceil(arr.length / pageSize)"
   :total-visible="6"
></v-pagination>