定义每页显示的数据数量(例如每页显示10条数据)。
根据总数据量和每页显示的数量计算出总页数。
给定当前页码,计算出当前页数据在左侧数据中的起始索引和结束索引。
使用起始索引和结束索引,从左侧数据中截取出当前页的数据。
将截取出的当前页数据展示给用户。
根据用户的操作(点击翻页按钮等),更新当前页码并重复步骤3至
- const leftData = [/* 据数组 */];
- const itemsPerPage = 10; // 每页显示的数据数量
-
- // 根据总数据量和每页显示的数量计算出总页数
- const totalPages = Math.ceil(leftData.length / itemsPerPage);
-
- function getPageData(page) {
- // 给定当前页码,计算出当前页数据在左侧数据中的起始索引和结束索引
- const startIndex = (page - 1) * itemsPerPage;
- const endIndex = page * itemsPerPage;
-
- // 使用起始索引和结束索引,从左侧数据中截取出当前页的数据
- const currentPageData = leftData.slice(startIndex, endIndex);
-
- // 返回当前页数据
- return currentPageData;
- }
-
- // 示例用法:
- const currentPage = 1; // 当前页码
- const currentPageData = getPageData(currentPage);
- console.log(currentPageData); // 打印当前页的数据