当前位置:首页 > VUE

vue实现列表分页

2026-01-16 00:29:48VUE

Vue 列表分页实现方法

基础分页实现

安装依赖(如使用第三方库)

npm install vue-paginate

模板部分示例

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>第 {{ currentPage }} 页</span>
    <button @click="nextPage" :disabled="currentPage === pageCount">下一页</button>
  </div>
</template>

脚本部分逻辑

export default {
  data() {
    return {
      items: [], // 原始数据
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    pageCount() {
      return Math.ceil(this.items.length / this.itemsPerPage)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.items.slice(start, end)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.pageCount) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}

使用Element UI分页组件

安装Element UI

npm install element-ui

模板示例

vue实现列表分页

<template>
  <div>
    <el-table :data="tableData">
      <!-- 表格列定义 -->
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next">
    </el-pagination>
  </div>
</template>

脚本逻辑

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    fetchData() {
      // 模拟API请求
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      this.tableData = mockData.slice(start, end)
      this.total = mockData.length
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    }
  },
  created() {
    this.fetchData()
  }
}

服务器端分页实现

API请求示例

methods: {
  async fetchPaginatedData() {
    const response = await axios.get('/api/items', {
      params: {
        page: this.currentPage,
        limit: this.itemsPerPage
      }
    })
    this.items = response.data.items
    this.totalItems = response.data.total
  }
}

分页参数处理

vue实现列表分页

watch: {
  currentPage(newVal) {
    this.fetchPaginatedData()
  }
}

分页样式优化

添加CSS样式

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.page-item {
  margin: 0 5px;
  cursor: pointer;
}
.active {
  font-weight: bold;
  color: #409EFF;
}

高级功能实现

添加页码跳转

<input v-model.number="goToPage" type="number" min="1" :max="pageCount">
<button @click="goToSpecificPage">跳转</button>

对应方法

methods: {
  goToSpecificPage() {
    if (this.goToPage >= 1 && this.goToPage <= this.pageCount) {
      this.currentPage = this.goToPage
    }
  }
}

标签: 分页列表
分享给朋友:

相关文章

vue router实现分页

vue router实现分页

Vue Router 实现分页的方法 在 Vue.js 中,可以通过 Vue Router 实现分页功能,通常需要结合路由参数和动态路由匹配。以下是几种常见的实现方式: 使用查询参数实现分页 在路…

js实现分页

js实现分页

分页的基本实现思路 在JavaScript中实现分页功能通常需要结合前端和后端逻辑。前端负责渲染分页控件和处理用户交互,后端负责提供分页数据。 前端分页实现 纯前端分页适用于数据量较小的情况,可以…

h5实现展开列表

h5实现展开列表

使用HTML5和CSS实现展开列表 在HTML5中,可以通过结合CSS和JavaScript实现展开列表效果。以下是几种常见的方法: 方法1:使用details和summary标签 HTML5提供…

分页实现vue

分页实现vue

分页实现方法(Vue) 使用Element UI分页组件 安装Element UI库后,可以直接使用其分页组件。在模板中添加el-pagination,通过v-model绑定当前页码,设置page-s…

react 如何分页

react 如何分页

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

jquery 分页

jquery 分页

实现 jQuery 分页的方法 客户端分页实现 使用 jQuery 实现客户端分页的核心思路是通过 JavaScript 控制数据的显示与隐藏。假设有一个包含数据的 HTML 列表或表格: //…