// 简单的 JavaScript 分页示例
// 假设我们有一个包含大量数据的数组
const data = Array.from({ length: 100 }, (_, i) => `Item ${i + 1}`);
// 每页显示的项目数量
const itemsPerPage = 10;
// 当前页码
let currentPage = 1;
// 获取分页数据的函数
function getPagedData(data, page, itemsPerPage) {
const startIndex = (page - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
return data.slice(startIndex, endIndex);
}
// 显示分页数据的函数
function displayPagedData() {
const pagedData = getPagedData(data, currentPage, itemsPerPage);
console.log(`Page ${currentPage}:`, pagedData);
}
// 切换页面的函数
function changePage(newPage) {
if (newPage >= 1 && newPage <= Math.ceil(data.length / itemsPerPage)) {
currentPage = newPage;
displayPagedData();
} else {
console.log('Invalid page number');
}
}
// 初始化显示第一页的数据
displayPagedData();
// 示例:切换到第2页
changePage(2);
// 示例:切换到第5页
changePage(5);
data,模拟大量数据。itemsPerPage 和当前页码 currentPage。getPagedData 函数根据当前页码和每页显示的项目数量,从数据数组中提取相应的子数组。displayPagedData 函数调用 getPagedData 并将结果打印到控制台。changePage 函数允许用户切换到指定的页码,并检查页码是否有效。你可以根据需要进一步扩展这个示例,例如添加分页按钮、前后翻页功能等。
上一篇:js markdown
下一篇:js callback
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站