当前位置:首页 > VUE

Vue如何实现结果分页

2026-02-21 19:19:48VUE

Vue 实现结果分页的方法

使用 Element UI 的分页组件

Element UI 提供了现成的分页组件 el-pagination,可以快速实现分页功能。安装 Element UI 后,在组件中引入并使用分页组件。

<template>
  <div>
    <el-table :data="currentPageData">
      <!-- 表格内容 -->
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="totalData.length"
      layout="prev, pager, next"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      totalData: [], // 所有数据
      currentPage: 1,
      pageSize: 10,
    };
  },
  computed: {
    currentPageData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.totalData.slice(start, end);
    },
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
    },
  },
};
</script>

手动实现分页逻辑

如果不使用 UI 框架,可以通过计算属性手动实现分页逻辑。

Vue如何实现结果分页

<template>
  <div>
    <table>
      <tr v-for="item in currentPageData" :key="item.id">
        <td>{{ item.name }}</td>
      </tr>
    </table>
    <button @click="prevPage">上一页</button>
    <span>{{ currentPage }}</span>
    <button @click="nextPage">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      totalData: [], // 所有数据
      currentPage: 1,
      pageSize: 10,
    };
  },
  computed: {
    currentPageData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.totalData.slice(start, end);
    },
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    },
    nextPage() {
      if (this.currentPage < Math.ceil(this.totalData.length / this.pageSize)) {
        this.currentPage++;
      }
    },
  },
};
</script>

使用第三方库

如果需要更复杂的分页功能,可以考虑使用第三方库如 vue-paginatev-pagination。这些库提供了更多自定义选项和功能。

安装 vue-paginate

Vue如何实现结果分页

npm install vue-paginate

使用示例:

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
    <paginate
      v-model="currentPage"
      :page-count="totalPages"
      :click-handler="changePage"
      :prev-text="'Prev'"
      :next-text="'Next'"
    >
    </paginate>
  </div>
</template>

<script>
import Paginate from 'vue-paginate';
export default {
  components: {
    Paginate,
  },
  data() {
    return {
      totalData: [], // 所有数据
      currentPage: 1,
      pageSize: 10,
    };
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.totalData.slice(start, end);
    },
    totalPages() {
      return Math.ceil(this.totalData.length / this.pageSize);
    },
  },
  methods: {
    changePage(pageNum) {
      this.currentPage = pageNum;
    },
  },
};
</script>

后端分页

如果数据量很大,建议在后端实现分页逻辑,前端仅发送分页参数并接收分页后的数据。

<template>
  <div>
    <table>
      <tr v-for="item in data" :key="item.id">
        <td>{{ item.name }}</td>
      </tr>
    </table>
    <el-pagination
      @current-change="fetchData"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next"
    >
    </el-pagination>
  </div>
</template>

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

分享给朋友:

相关文章

vue如何实现放大缩小

vue如何实现放大缩小

Vue实现放大缩小功能的方法 在Vue中实现元素的放大缩小功能,可以通过CSS变换、第三方库或直接操作DOM样式来实现。以下是几种常见的方法: 使用CSS transform属性 通过绑定Vue的d…

vue 实现分页

vue 实现分页

分页实现方法 在Vue中实现分页功能,可以通过计算属性和方法结合来实现。以下是一个典型的分页实现方式: 模板部分 <template> <div> <ul…

js双击事件如何实现

js双击事件如何实现

实现双击事件的方法 在JavaScript中,可以通过监听dblclick事件或手动检测两次点击的时间间隔来实现双击事件。以下是几种常见的方法: 使用原生dblclick事件 element…

vue如何实现排序

vue如何实现排序

实现数组排序 在Vue中可以通过计算属性或方法对数组进行排序。使用JavaScript的sort()方法结合Vue的响应式特性实现动态排序。 data() { return { item…

java是如何实现跨平台的

java是如何实现跨平台的

Java 跨平台的实现原理 Java 实现跨平台的核心机制基于 “一次编写,到处运行” 的理念,主要通过以下技术实现: 字节码与 JVM Java 源代码编译后生成字节码(.class 文件),而非…

vue如何实现轮播

vue如何实现轮播

使用 Vue 实现轮播 使用第三方库(推荐) Vue 生态中有许多成熟的轮播组件库,例如 vue-awesome-swiper 或 swiper。以下是使用 vue-awesome-swiper 的示…