function getPageNum(item, limit = 5) {
return Math.ceil(item / limit);
}
console.log(`Item 1 is on page ${getPageNum(1)}`);
console.log(`Item 5 is on page ${getPageNum(5)}`);
console.log(`Item 7 is on page ${getPageNum(7)}`);
console.log(`Item 10 is on page ${getPageNum(10)}`);
console.log(`Item 13 is on page ${getPageNum(13)}`);
console.log(`Item 15 is on page ${getPageNum(15)}`);
console.log(`Item 16 is on page ${getPageNum(16)}`);
console.log(`Item 20 is on page ${getPageNum(20)}`);
console.log(`Item 21 is on page ${getPageNum(21)}`);
2条答案
按热度按时间snvhrwxg1#
你很接近了列表中的当前项目不除以项目数,而是除以每页允许的项目数(或限制)。
wljmcqd82#
您可以使用以下代码根据ID获取页码:
变量pageForThisProduct将存储页码,您可以根据功能进行显示。