当前位置:首页 > JavaScript

js分页实现前端实现

2026-04-06 15:33:20JavaScript

分页实现的基本逻辑

分页功能通常需要结合后端API返回的数据总量和当前页码,前端负责计算分页参数并渲染分页器组件。核心逻辑包括计算总页数、生成页码按钮、处理翻页事件等。

基于纯JavaScript的实现

// 分页配置
const paginationConfig = {
  currentPage: 1,
  itemsPerPage: 10,
  totalItems: 100,
  maxVisiblePages: 5
};

// 生成分页HTML
function renderPagination() {
  const totalPages = Math.ceil(paginationConfig.totalItems / paginationConfig.itemsPerPage);
  const container = document.getElementById('pagination-container');
  container.innerHTML = '';

  // 上一页按钮
  if (paginationConfig.currentPage > 1) {
    container.appendChild(createPageButton('«', paginationConfig.currentPage - 1));
  }

  // 页码按钮
  const startPage = Math.max(1, paginationConfig.currentPage - Math.floor(paginationConfig.maxVisiblePages / 2));
  const endPage = Math.min(totalPages, startPage + paginationConfig.maxVisiblePages - 1);

  for (let i = startPage; i <= endPage; i++) {
    container.appendChild(createPageButton(i, i));
  }

  // 下一页按钮
  if (paginationConfig.currentPage < totalPages) {
    container.appendChild(createPageButton('»', paginationConfig.currentPage + 1));
  }
}

// 创建页码按钮元素
function createPageButton(text, pageNum) {
  const button = document.createElement('button');
  button.textContent = text;
  button.className = pageNum === paginationConfig.currentPage ? 'active' : '';
  button.addEventListener('click', () => {
    paginationConfig.currentPage = pageNum;
    renderPagination();
    fetchData(); // 实际项目中这里调用数据获取函数
  });
  return button;
}

结合数据加载的实现

实际项目中需要配合数据请求实现完整分页流程:

async function fetchData() {
  const response = await fetch(`/api/data?page=${paginationConfig.currentPage}&limit=${paginationConfig.itemsPerPage}`);
  const result = await response.json();

  paginationConfig.totalItems = result.total;
  renderData(result.items);
  renderPagination();
}

function renderData(items) {
  const container = document.getElementById('data-container');
  container.innerHTML = items.map(item => `
    <div class="item">${item.name}</div>
  `).join('');
}

分页样式建议

#pagination-container {
  display: flex;
  gap: 5px;
  margin-top: 20px;
}

#pagination-container button {
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
}

#pagination-container button.active {
  background: #007bff;
  color: white;
  border-color: #007bff;
}

常见分页模式扩展

无限滚动分页

window.addEventListener('scroll', () => {
  if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 500) {
    if (!isLoading && hasMore) {
      loadNextPage();
    }
  }
});

表格分页组件

js分页实现前端实现

function createTablePagination(tableId, options) {
  const table = document.getElementById(tableId);
  // 实现表格特定分页逻辑
}

性能优化建议

  • 使用防抖技术处理快速翻页
  • 缓存已加载的页面数据
  • 虚拟滚动处理大数据量
  • 预加载下一页数据

以上实现可以根据具体项目需求进行调整,核心是将分页状态管理与UI渲染分离,保持组件可复用性。实际开发中建议使用现有库如pagination.js或框架特定组件以获得更完整的功能支持。

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

相关文章

js实现图片轮播

js实现图片轮播

实现基础图片轮播 使用HTML、CSS和JavaScript创建一个简单的图片轮播。HTML部分定义轮播容器和图片,CSS设置样式和动画效果,JavaScript处理轮播逻辑。 <div cl…

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

react 如何分页

react 如何分页

分页实现方法 在React中实现分页功能可以通过多种方式完成,具体取决于数据来源(如API或本地数据)和UI库的选择。以下是常见的实现方法: 使用本地数据分页 对于存储在组件状态或Context中…

js实现pdf在线预览

js实现pdf在线预览

使用PDF.js实现PDF在线预览 PDF.js是由Mozilla开发的一个开源JavaScript库,可以在网页中直接渲染PDF文件。以下是实现PDF在线预览的步骤: 引入PDF.js库 在HT…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

js实现分页功能代码

js实现分页功能代码

分页功能实现方法 客户端分页(前端处理数据) 适用于数据量较小且已全部加载到前端的情况。 // 示例数据 const data = Array.from({length: 100}, (_, i)…