当前位置:首页 > JavaScript

js 分页实现

2026-02-01 16:14:26JavaScript

分页实现的基本原理

分页的核心是将大量数据分割成多个小块,每次只加载或显示其中一部分。通常需要计算总页数、当前页码、每页数据量等参数,结合前端渲染或后端接口实现。

前端分页实现

适用于数据量较小且已全部加载到前端的情况,通过数组切片实现分页。

// 示例数据
const allData = [...Array(100).keys()].map(i => ({ id: i, name: `Item ${i}` }));

function paginateFrontend(data, currentPage, itemsPerPage) {
  const startIndex = (currentPage - 1) * itemsPerPage;
  return data.slice(startIndex, startIndex + itemsPerPage);
}

// 使用示例
const currentPage = 2;
const itemsPerPage = 10;
const pageData = paginateFrontend(allData, currentPage, itemsPerPage);

后端分页实现

通过API请求实现,常用limitoffset参数(或pagesize)。

async function fetchPaginatedData(page, size) {
  const response = await fetch(`/api/items?page=${page}&size=${size}`);
  return response.json();
}

// 使用示例
fetchPaginatedData(1, 10).then(data => {
  console.log(data.items, data.totalPages);
});

分页组件示例

常见UI分页组件实现逻辑(以React为例):

function Pagination({ currentPage, totalPages, onPageChange }) {
  return (
    <div className="pagination">
      <button 
        disabled={currentPage === 1}
        onClick={() => onPageChange(currentPage - 1)}
      >Previous</button>

      {Array.from({ length: totalPages }, (_, i) => (
        <button
          key={i}
          className={currentPage === i + 1 ? 'active' : ''}
          onClick={() => onPageChange(i + 1)}
        >{i + 1}</button>
      ))}

      <button
        disabled={currentPage === totalPages}
        onClick={() => onPageChange(currentPage + 1)}
      >Next</button>
    </div>
  );
}

分页参数计算

计算分页相关参数的通用方法:

function calculatePagination(totalItems, currentPage, itemsPerPage) {
  const totalPages = Math.ceil(totalItems / itemsPerPage);
  currentPage = Math.max(1, Math.min(currentPage, totalPages));
  const startIndex = (currentPage - 1) * itemsPerPage;

  return {
    currentPage,
    totalPages,
    startIndex,
    endIndex: Math.min(startIndex + itemsPerPage - 1, totalItems - 1)
  };
}

分页样式优化

CSS样式建议(配合上述组件):

.pagination {
  display: flex;
  gap: 5px;
}
.pagination button {
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: white;
}
.pagination button.active {
  background: #007bff;
  color: white;
}
.pagination button:disabled {
  opacity: 0.5;
}

性能优化建议

大数据量分页时推荐使用无限滚动(Intersection Observer实现)或后端游标分页(cursor-based pagination),避免传统分页的性能问题。对于SQL数据库,推荐使用LIMIT offset, count语法时配合索引优化。

js 分页实现

标签: 分页js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 // 方…

js实现轮播图

js实现轮播图

轮播图实现方法 基础HTML结构 <div class="slider"> <div class="slides"> <img src="image1.jpg…

分页用vue实现

分页用vue实现

分页用 Vue 实现 在 Vue 中实现分页功能通常需要结合数据管理和 UI 组件。以下是几种常见的方法: 使用 Vue 组件库的分页组件 许多 Vue UI 组件库(如 Element UI、Va…

vue实现分页功能

vue实现分页功能

Vue 分页功能实现 在 Vue 中实现分页功能通常需要结合后端 API 或前端数据分页逻辑。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在 Vue 组件中处理分页逻辑…

js实现动画

js实现动画

使用 CSS 动画与 JavaScript 控制 通过 JavaScript 动态添加或移除 CSS 类来触发动画。CSS 定义关键帧(@keyframes),JavaScript 通过 classL…

js实现倒计时

js实现倒计时

使用 setInterval 实现倒计时 通过 setInterval 定时器每秒更新剩余时间,适用于简单倒计时场景。 function countdown(seconds, callbac…