当前位置:首页 > 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 框架,可以通过计算属性手动实现分页逻辑。

<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

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>

后端分页

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

Vue如何实现结果分页

<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 提供了多种方式来实现功能,具体取决于需求。以下是一些常见场景的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。在组件中可以通过 pro…

vue分页实现

vue分页实现

Vue 分页实现方法 基础分页组件实现 创建一个简单的分页组件,包含上一页、下一页和页码按钮。组件接收当前页码、总页数等参数,通过事件通知父组件页码变化。 <template> &l…

如何实现语音react

如何实现语音react

语音识别基础设置 在React中实现语音识别功能通常需要借助浏览器的Web Speech API或第三方库。Web Speech API提供了SpeechRecognition接口,允许应用程序直接访…

jquery分页

jquery分页

jQuery分页的实现方法 jQuery分页可以通过多种方式实现,以下是几种常见的方法: 使用插件实现分页 jQuery有许多优秀的分页插件,如jQuery Pagination、DataTabl…

vue实现后台分页

vue实现后台分页

Vue 实现后台分页的方法 分页组件与数据绑定 在 Vue 中实现后台分页通常需要结合分页组件和后端 API。使用 el-pagination(Element UI)或自定义分页组件,通过 v-mod…

vue如何实现截图

vue如何实现截图

Vue 实现截图的方法 在 Vue 中实现截图功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 html2canvas 库 html2canvas 是一个流行的 JavaScript 库,…