当前位置:首页 > 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组件传值实现分页

vue组件传值实现分页

Vue 组件传值实现分页 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方法: 使用props和$emit 父组件通过props向子组件传递数据,子组件通过$emit向…

vue  实现分页

vue 实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。分页的核心逻辑包括计算总页数、处理当前页码变化以及触发数据请求。 分页组件示例 使用Element UI的分页…

vue 分页的实现

vue 分页的实现

Vue 分页的实现方法 使用 Element UI 的分页组件 安装 Element UI: npm install element-ui 在 Vue 项目中引入 Element UI 的分页组件:…

网页制作css 列表

网页制作css 列表

创建CSS列表样式 使用CSS可以自定义HTML列表(有序列表<ol>和无序列表<ul>)的外观,包括项目符号、缩进、颜色等。以下是一些常见的列表样式方法: 基础列表样式 通…

vue实现分页

vue实现分页

Vue 实现分页的方法 使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 el-pagination,适合快速集成到 Vue 项目中。 安装 Element UI…

vue实现商品列表

vue实现商品列表

Vue实现商品列表的方法 使用Vue实现商品列表需要结合数据绑定、组件化和状态管理。以下是实现商品列表的几种常见方法: 基础数据绑定实现 在Vue组件中定义商品数据数组,使用v-for指令循环渲染商…