当前位置:首页 > JavaScript

js 实现分页

2026-01-13 14:00:19JavaScript

实现分页的基本逻辑

分页功能通常需要后端返回数据总量或总页数,前端根据当前页码和每页条数截取对应数据。以下是一个基于JavaScript的简单分页实现方案:

前端分页实现

假设已有从后端获取的完整数据数组allData,可通过以下方式实现分页:

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

// 示例使用
const allData = [/* 你的数据数组 */]
const currentPage = 2
const pageSize = 10
const pageData = paginate(allData, currentPage, pageSize)

后端分页实现

更常见的做法是让后端处理分页逻辑,前端只需传递页码和每页条数:

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

// 示例使用
const pageInfo = {
  currentPage: 1,
  pageSize: 10
}

fetchPaginatedData(pageInfo.currentPage, pageInfo.pageSize)
  .then(data => {
    // 处理分页数据
  })

分页组件实现

完整的分页组件通常包含页码导航和状态显示:

class Pagination {
  constructor(totalItems, itemsPerPage, currentPage = 1) {
    this.totalItems = totalItems
    this.itemsPerPage = itemsPerPage
    this.currentPage = currentPage
    this.totalPages = Math.ceil(totalItems / itemsPerPage)
  }

  getCurrentPageData(data) {
    const start = (this.currentPage - 1) * this.itemsPerPage
    const end = start + this.itemsPerPage
    return data.slice(start, end)
  }

  goToPage(page) {
    if (page < 1) page = 1
    if (page > this.totalPages) page = this.totalPages
    this.currentPage = page
  }
}

分页UI示例

结合HTML的分页控件实现:

js 实现分页

<div id="pagination-controls">
  <button id="prev">上一页</button>
  <span id="page-info">第1页/共10页</span>
  <button id="next">下一页</button>
</div>

<script>
  const pagination = new Pagination(100, 10)
  const prevBtn = document.getElementById('prev')
  const nextBtn = document.getElementById('next')
  const pageInfo = document.getElementById('page-info')

  function updateUI() {
    pageInfo.textContent = `第${pagination.currentPage}页/共${pagination.totalPages}页`
    prevBtn.disabled = pagination.currentPage === 1
    nextBtn.disabled = pagination.currentPage === pagination.totalPages
  }

  prevBtn.addEventListener('click', () => {
    pagination.goToPage(pagination.currentPage - 1)
    updateUI()
    // 加载新页数据...
  })

  nextBtn.addEventListener('click', () => {
    pagination.goToPage(pagination.currentPage + 1)
    updateUI()
    // 加载新页数据...
  })

  updateUI()
</script>

分页优化建议

  • 对于大数据量,推荐使用后端分页而非前端分页
  • 添加页面跳转输入框,允许用户直接输入页码
  • 实现页码过多时的省略显示(如显示1...5,6,7...20)
  • 考虑添加每页条数选择器
  • 在URL中保持分页状态,便于分享和刷新后恢复

以上实现方案可以根据具体需求进行调整和扩展,核心逻辑是通过页码和每页条数计算数据截取范围或向后端请求对应数据。

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

相关文章

vue实现后台分页

vue实现后台分页

实现后台分页的方法 在Vue中实现后台分页通常需要结合前端和后端逻辑,后端负责处理数据分页,前端负责展示分页数据和交互。以下是具体实现方法: 后端API设计 后端需要提供支持分页的API接口,通常包…

vue搜索过后实现分页

vue搜索过后实现分页

实现 Vue 搜索后分页的方法 数据绑定与搜索逻辑 在 Vue 组件中定义数据属性,包括原始数据列表、搜索关键词、分页相关参数: data() { return { originalLi…

js实现论坛

js实现论坛

实现论坛的基本功能 使用JavaScript实现一个论坛需要结合前端和后端技术。前端可以使用React、Vue或Angular等框架,后端可以选择Node.js配合Express或Koa框架。数据库可…

js实现驼峰

js实现驼峰

实现驼峰命名的几种方法 使用正则表达式和字符串替换 通过正则表达式匹配字符串中的特定模式(如下划线或短横线),并将其后的字母转换为大写,同时移除分隔符。 function toCamelCase(s…

js实现游标

js实现游标

使用JavaScript实现游标 在JavaScript中,可以通过操作DOM元素的cursor样式属性来实现自定义游标效果。以下是几种常见的实现方法: 修改默认鼠标指针样式 通过CSS的curso…

js钟表实现

js钟表实现

实现JavaScript钟表的基本方法 创建一个简单的JavaScript钟表可以通过以下步骤完成,涵盖数字和模拟两种形式。 数字钟表实现 HTML结构只需一个显示时间的容器: <div i…