当前位置:首页 > 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
    }
  }
}

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

相关文章

react 如何分页

react 如何分页

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

vue实现列表添加

vue实现列表添加

使用 Vue 实现列表添加功能 数据绑定与列表渲染 在 Vue 中,通过 v-for 指令可以轻松实现列表渲染。首先需要定义一个数组来存储列表数据,并在模板中使用 v-for 循环渲染。 <t…

vue 实现列表多选

vue 实现列表多选

Vue 实现列表多选的方法 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动处理多选逻辑。适用于复选框(checkbox)场景。 <template> &…

vue 实现数据分页

vue 实现数据分页

实现数据分页的基本思路 在Vue中实现数据分页通常需要结合前端分页逻辑和后端API支持。前端分页适合数据量较小的情况,后端分页适合大数据量场景。 前端分页实现 创建一个计算属性来分割当前页数据:…

vue分页实现原理

vue分页实现原理

Vue 分页实现原理 Vue 分页的核心原理是通过计算当前页的数据范围,动态渲染数据列表并控制分页组件的交互。以下是关键实现步骤: 数据分片计算 分页需要根据当前页码和每页条数截取数据。假设原始数据…

vue实现影片列表

vue实现影片列表

Vue 实现影片列表 使用 Vue CLI 创建项目 确保已安装 Vue CLI,通过以下命令创建新项目: vue create movie-list cd movie-list 安装依赖 根据…