当前位置:首页 > VUE

vue实现table表格分页

2026-02-21 17:13:59VUE

Vue 实现表格分页的方法

使用 Element UI 的分页组件

安装 Element UI 后,可以直接使用其提供的表格和分页组件。

vue实现table表格分页

<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
    </el-table>
    <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>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    fetchData() {
      // 模拟 API 调用
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.tableData = this.allData.slice(start, end);
    }
  },
  created() {
    // 模拟数据
    this.allData = Array.from({ length: 100 }, (_, i) => ({
      date: `2023-01-${i + 1}`,
      name: `用户${i + 1}`
    }));
    this.total = this.allData.length;
    this.fetchData();
  }
}
</script>

自定义分页逻辑

如果不使用 UI 库,可以手动实现分页功能。

vue实现table表格分页

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>日期</th>
          <th>姓名</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in paginatedData" :key="item.name">
          <td>{{ item.date }}</td>
          <td>{{ item.name }}</td>
        </tr>
      </tbody>
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === pageCount">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      totalItems: 100
    }
  },
  computed: {
    pageCount() {
      return Math.ceil(this.totalItems / this.pageSize);
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.tableData.slice(start, end);
    }
  },
  methods: {
    prevPage() {
      this.currentPage--;
    },
    nextPage() {
      this.currentPage++;
    }
  },
  created() {
    // 模拟数据
    this.tableData = Array.from({ length: 100 }, (_, i) => ({
      date: `2023-01-${i + 1}`,
      name: `用户${i + 1}`
    }));
  }
}
</script>

使用 Vue 3 的 Composition API

在 Vue 3 中,可以使用 Composition API 实现分页逻辑。

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>日期</th>
          <th>姓名</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in paginatedData" :key="item.name">
          <td>{{ item.date }}</td>
          <td>{{ item.name }}</td>
        </tr>
      </tbody>
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === pageCount">下一页</button>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue';

export default {
  setup() {
    const tableData = ref([]);
    const currentPage = ref(1);
    const pageSize = ref(10);
    const totalItems = ref(100);

    const pageCount = computed(() => Math.ceil(totalItems.value / pageSize.value));
    const paginatedData = computed(() => {
      const start = (currentPage.value - 1) * pageSize.value;
      const end = start + pageSize.value;
      return tableData.value.slice(start, end);
    });

    const prevPage = () => {
      if (currentPage.value > 1) currentPage.value--;
    };
    const nextPage = () => {
      if (currentPage.value < pageCount.value) currentPage.value++;
    };

    // 模拟数据
    tableData.value = Array.from({ length: 100 }, (_, i) => ({
      date: `2023-01-${i + 1}`,
      name: `用户${i + 1}`
    }));

    return {
      tableData,
      currentPage,
      pageSize,
      totalItems,
      pageCount,
      paginatedData,
      prevPage,
      nextPage
    };
  }
}
</script>

注意事项

  • 分页逻辑的核心是计算当前页的数据范围,通常使用 slice 方法截取数组。
  • 在实际项目中,数据通常来自后端 API,需要在分页变化时重新请求数据。
  • 分页组件应提供足够的用户反馈,例如禁用按钮、显示总页数等。

标签: 分页表格
分享给朋友:

相关文章

vue表格实现导出

vue表格实现导出

使用 vue-json-excel 插件 安装插件: npm install vue-json-excel --save 在组件中引入并注册: import JsonExcel from 'vue…

vue分页实现原理

vue分页实现原理

Vue 分页实现原理 Vue 分页的核心原理是通过计算当前页的数据范围,动态渲染数据列表并控制分页组件的交互。以下是关键实现步骤: 数据分片计算 分页需要根据当前页码和每页条数截取数据。假设原始数据…

vue实现表格输出

vue实现表格输出

Vue 实现表格输出的方法 使用原生表格标签 在 Vue 模板中可以直接使用 HTML 原生表格标签(<table>、<tr>、<td>等)渲染数据。通过 v-fo…

vue实现表格滚动

vue实现表格滚动

Vue 实现表格滚动的方法 使用 CSS 固定表头并滚动内容 通过 CSS 设置表格容器的固定高度和 overflow-y: auto,实现内容区域的滚动。表头部分使用 position: stick…

vue实现分页表

vue实现分页表

Vue 实现分页表格 在 Vue 中实现分页表格通常需要结合表格组件和分页组件,以下是常见的实现方法: 使用 Element UI 实现分页表格 安装 Element UI: npm instal…

vue 实现简单表格

vue 实现简单表格

以下是在 Vue 中实现简单表格的几种方法: 使用原生表格标签 通过 Vue 的数据绑定功能渲染表格数据: <template> <table> <the…