当前位置:首页 > VUE

vue中分页怎么实现

2026-01-21 18:10:23VUE

使用Element UI的分页组件

Element UI提供了现成的分页组件el-pagination,适用于Vue 2项目。安装Element UI后,直接引入分页组件并绑定数据即可。

<template>
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="[10, 20, 30, 40]"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total">
  </el-pagination>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 100
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    fetchData() {
      // 根据currentPage和pageSize请求数据
    }
  }
}
</script>

使用Ant Design Vue的分页组件

Ant Design Vue的a-pagination组件适用于Vue 3项目,功能与Element UI类似。

<template>
  <a-pagination
    v-model:current="currentPage"
    v-model:pageSize="pageSize"
    :total="total"
    show-size-changer
    @change="handlePageChange"
  />
</template>

<script>
import { ref } from 'vue';
export default {
  setup() {
    const currentPage = ref(1);
    const pageSize = ref(10);
    const total = ref(100);

    const handlePageChange = () => {
      // 根据currentPage和pageSize请求数据
    };

    return { currentPage, pageSize, total, handlePageChange };
  }
}
</script>

手动实现分页逻辑

如果不依赖UI库,可以通过计算属性手动实现分页逻辑。假设有一个数据数组listData,需要按每页pageSize条数据进行分割。

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

<script>
export default {
  data() {
    return {
      listData: [], // 原始数据
      currentPage: 1,
      pageSize: 5
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.listData.slice(start, end);
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--;
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++;
    }
  }
}
</script>

结合后端API实现分页

实际项目中,分页通常需要与后端API交互。通过传递pagepageSize参数获取对应数据。

methods: {
  async fetchData() {
    const params = {
      page: this.currentPage,
      pageSize: this.pageSize
    };
    const res = await axios.get('/api/data', { params });
    this.listData = res.data.list;
    this.total = res.data.total;
  }
}

分页样式自定义

如果需要自定义分页样式,可以通过覆盖UI库的CSS或手动编写样式。例如修改Element UI分页按钮颜色:

.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #ff6b6b;
}

vue中分页怎么实现

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

相关文章

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&…

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用于…

vue聊天实现

vue聊天实现

Vue 聊天应用实现步骤 创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目,安装必要依赖(如 Vue 3、Element UI 或 Vuetify 用于 UI 组件)。 npm c…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSel…