当前位置:首页 > VUE

vue 实现分页

2026-03-07 04:53:54VUE

Vue 实现分页的方法

在 Vue 中实现分页功能可以通过多种方式完成,以下是几种常见的实现方法:

使用计算属性分页

通过计算属性动态计算当前页的数据,结合页码切换逻辑实现分页。

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <button @click="prevPage" :disabled="currentPage === 1">Previous</button>
    <span>Page {{ currentPage }} of {{ totalPages }}</span>
    <button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
  </div>
</template>

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

使用第三方库(如 Element UI)

Element UI 提供了现成的分页组件,可以快速集成到 Vue 项目中。

<template>
  <div>
    <el-table :data="paginatedData">
      <el-table-column prop="name" label="Name"></el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="itemsPerPage"
      :total="totalItems"
      layout="prev, pager, next"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [], // 原始数据
      currentPage: 1,
      itemsPerPage: 10
    };
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.items.slice(start, end);
    },
    totalItems() {
      return this.items.length;
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
    }
  }
};
</script>

使用 Vuex 管理分页状态

如果项目使用了 Vuex,可以将分页状态集中管理。

// store.js
export default new Vuex.Store({
  state: {
    items: [],
    currentPage: 1,
    itemsPerPage: 10
  },
  getters: {
    paginatedData: (state) => {
      const start = (state.currentPage - 1) * state.itemsPerPage;
      const end = start + state.itemsPerPage;
      return state.items.slice(start, end);
    },
    totalPages: (state) => Math.ceil(state.items.length / state.itemsPerPage)
  },
  mutations: {
    setCurrentPage(state, page) {
      state.currentPage = page;
    }
  }
});
<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <button @click="prevPage" :disabled="currentPage === 1">Previous</button>
    <span>Page {{ currentPage }} of {{ totalPages }}</span>
    <button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
  </div>
</template>

<script>
import { mapGetters, mapState, mapMutations } from 'vuex';

export default {
  computed: {
    ...mapGetters(['paginatedData', 'totalPages']),
    ...mapState(['currentPage'])
  },
  methods: {
    ...mapMutations(['setCurrentPage']),
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.setCurrentPage(this.currentPage + 1);
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.setCurrentPage(this.currentPage - 1);
      }
    }
  }
};
</script>

后端分页与 API 集成

对于大数据量,通常需要后端分页支持。通过 API 传递页码和每页条数。

vue 实现分页

<template>
  <div>
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}</li>
    </ul>
    <el-pagination
      @current-change="fetchData"
      :current-page="currentPage"
      :page-size="itemsPerPage"
      :total="totalItems"
      layout="prev, pager, next"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      currentPage: 1,
      itemsPerPage: 10,
      totalItems: 0
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData(page = 1) {
      this.currentPage = page;
      axios.get('/api/items', {
        params: {
          page: this.currentPage,
          limit: this.itemsPerPage
        }
      }).then(response => {
        this.items = response.data.items;
        this.totalItems = response.data.total;
      });
    }
  }
};
</script>

以上方法涵盖了从简单前端分页到复杂后端分页的实现,可以根据具体需求选择适合的方式。

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

相关文章

vue实现签约合作弹窗

vue实现签约合作弹窗

实现签约合作弹窗的步骤 使用Vue实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDia…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue实现抽奖

vue实现抽奖

Vue 实现抽奖功能 基本抽奖逻辑 创建一个Vue组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。 <template> <div> <…

vue 实现fadeout

vue 实现fadeout

Vue 实现淡出效果 在 Vue 中实现淡出(fade-out)效果可以通过 CSS 过渡或动画结合 Vue 的动态渲染功能完成。以下是几种常见方法: 使用 Vue 过渡(Transition)组件…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…