【发布时间】:2016-01-05 03:13:32
【问题描述】:
我目前正在使用 vuejs 渲染分页。这是我的 algolia 数据。
我正在检查用户是否按下了上一个按钮/下一个按钮或直接输入(1、2、3 等)
setPage: function(page) {
if(page === 'previous') {
// go back one page
this.currentPage--;
this.helper.setCurrentPage(this.currentPage).search();
} else if(page === 'next') {
// go forward one page
this.currentPage++;
this.helper.setCurrentPage(this.currentPage).search();
} else {
// change the currentPage with direct select(no prev/next button)
this.helper.setCurrentPage(page).search();
}
// update the currentPage
this.currentPage = this.helper.getCurrentPage();
}
这很好用,但我只有 0 - 4 页(页面从 0 开始)。如何确保我的 currentPage var 不会出现负数或超出我拥有的最大页面数?
我想使用另一个 if 语句可能会起作用,但感觉有点草率。我正在学习 javascript 和 vue.js,所以我想知道是否有更好的方法。
【问题讨论】:
-
if (page < MIN || page > MAX) throw new Error("That page does not exist!") -
取决于想要的行为...您是否希望它在结束时循环到开始?还是隐藏按钮?无论哪种方式都需要与某处可用的总数进行比较,而这并未在问题中显示
标签: javascript jquery json vue.js algolia