当前位置:首页 > VUE

vue实现table静态分页

2026-02-24 12:54:29VUE

Vue 实现 Table 静态分页

基础实现

使用 Vue 的 v-for 和计算属性对前端数据进行分页处理。假设数据存储在 dataList 中,通过 currentPagepageSize 控制分页。

vue实现table静态分页

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

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

分页组件封装

将分页逻辑抽离为可复用的组件,通过 props 传递数据和配置。

vue实现table静态分页

<!-- Pagination.vue -->
<template>
  <div class="pagination">
    <button @click="changePage(currentPage - 1)" :disabled="currentPage === 1">
      上一页
    </button>
    <span>{{ currentPage }} / {{ totalPages }}</span>
    <button 
      @click="changePage(currentPage + 1)" 
      :disabled="currentPage === totalPages"
    >
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    currentPage: { type: Number, required: true },
    totalPages: { type: Number, required: true }
  },
  methods: {
    changePage(page) {
      this.$emit('page-change', page);
    }
  }
};
</script>

使用第三方库

若需要更复杂的分页功能(如页码跳转),可使用 element-uiant-design-vue 等现成组件。

<template>
  <el-pagination
    :current-page="currentPage"
    :page-size="pageSize"
    :total="dataList.length"
    @current-change="handlePageChange"
  />
</template>

<script>
export default {
  methods: {
    handlePageChange(page) {
      this.currentPage = page;
    }
  }
};
</script>

性能优化

对于大数据量场景,使用虚拟滚动(如 vue-virtual-scroller)替代传统分页。

<template>
  <RecycleScroller
    :items="dataList"
    :item-size="50"
    key-field="id"
  >
    <template v-slot="{ item }">
      <div>{{ item.name }}</div>
    </template>
  </RecycleScroller>
</template>

关键点

  • 计算属性 paginatedData 实现数据切片
  • 分页按钮禁用状态通过 currentPagetotalPages 动态控制
  • 大数据量优先考虑虚拟滚动而非前端分页

标签: 分页静态
分享给朋友:

相关文章

vue实现分页条数

vue实现分页条数

实现分页条数的方法 在Vue中实现分页条数功能,通常需要结合分页组件和数据请求逻辑。以下是几种常见的实现方式: 使用Element UI的分页组件 Element UI提供了现成的分页组件,可以快…

vue 实现分页

vue 实现分页

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

vue  实现分页

vue 实现分页

实现分页功能 在Vue中实现分页功能通常需要结合后端API或前端数据处理。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,所有数据一次性加载到前端后进行分页处理: <…

vue实现后台分页

vue实现后台分页

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

vue实现列表分页

vue实现列表分页

Vue 列表分页实现方法 基础分页实现 安装依赖(如使用第三方库) npm install vue-paginate 模板部分示例 <template> <div>…

vue实现搜索分页

vue实现搜索分页

实现搜索分页的基本思路 在Vue中实现搜索分页功能通常需要结合以下几个关键点:数据绑定、搜索过滤、分页逻辑处理。以下是一个完整的实现方案。 数据准备与绑定 定义数据列表和分页相关变量,通常包括当前页…