当前位置:首页 > VUE

vue 实现表格分页

2026-01-18 06:01:34VUE

Vue 实现表格分页的方法

使用 Element UI 的分页组件

Element UI 提供了现成的分页组件 el-pagination,结合 el-table 可以快速实现表格分页功能。

安装 Element UI:

npm install element-ui

在 Vue 文件中引入组件:

import { ElTable, ElPagination } from 'element-ui';

模板部分:

<template>
  <div>
    <el-table :data="currentPageData" border>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" 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>

脚本部分:

vue 实现表格分页

export default {
  data() {
    return {
      tableData: [], // 全部数据
      currentPageData: [], // 当前页数据
      currentPage: 1,
      pageSize: 10,
      total: 0
    };
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.updateCurrentPageData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.updateCurrentPageData();
    },
    updateCurrentPageData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.currentPageData = this.tableData.slice(start, end);
    }
  },
  created() {
    // 模拟获取数据
    this.tableData = Array.from({ length: 100 }, (_, i) => ({
      name: `用户${i + 1}`,
      age: Math.floor(Math.random() * 50) + 18
    }));
    this.total = this.tableData.length;
    this.updateCurrentPageData();
  }
};

手动实现分页功能

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

模板部分:

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>姓名</th>
          <th>年龄</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in currentPageData" :key="item.id">
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
        </tr>
      </tbody>
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

脚本部分:

vue 实现表格分页

export default {
  data() {
    return {
      tableData: [],
      currentPageData: [],
      currentPage: 1,
      pageSize: 10,
      totalPages: 0
    };
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
        this.updateCurrentPageData();
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
        this.updateCurrentPageData();
      }
    },
    updateCurrentPageData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.currentPageData = this.tableData.slice(start, end);
    }
  },
  created() {
    this.tableData = Array.from({ length: 100 }, (_, i) => ({
      id: i + 1,
      name: `用户${i + 1}`,
      age: Math.floor(Math.random() * 50) + 18
    }));
    this.totalPages = Math.ceil(this.tableData.length / this.pageSize);
    this.updateCurrentPageData();
  }
};

样式部分:

table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
}
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

使用 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 currentPageData" :key="item.id">
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
        </tr>
      </tbody>
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

脚本部分:

import { ref, computed, onMounted } from 'vue';

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

    const currentPageData = 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 < totalPages.value) {
        currentPage.value++;
      }
    };

    onMounted(() => {
      tableData.value = Array.from({ length: 100 }, (_, i) => ({
        id: i + 1,
        name: `用户${i + 1}`,
        age: Math.floor(Math.random() * 50) + 18
      }));
      totalPages.value = Math.ceil(tableData.value.length / pageSize.value);
    });

    return {
      tableData,
      currentPage,
      pageSize,
      totalPages,
      currentPageData,
      prevPage,
      nextPage
    };
  }
};

以上方法提供了不同场景下的表格分页实现方案,可以根据项目需求选择合适的方式。

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

相关文章

vue日历表格实现

vue日历表格实现

Vue 日历表格实现方法 基础日历表格结构 使用 Vue 的模板语法构建日历表格框架,通过 v-for 循环生成日期单元格。核心是计算当前月份的天数及起始星期位置。 <template>…

vue组件传值实现分页

vue组件传值实现分页

Vue 组件传值实现分页 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方法: 使用props和$emit 父组件通过props向子组件传递数据,子组件通过$emit向…

vue实现表格复选

vue实现表格复选

Vue实现表格复选的方法 基础实现 在Vue中实现表格复选功能可以通过v-model绑定复选框的状态,结合v-for循环渲染表格数据。以下是一个基础示例: <template> &l…

vue实现子表格

vue实现子表格

Vue 实现子表格的方法 在 Vue 中实现子表格可以通过多种方式完成,以下是几种常见的方法: 使用嵌套组件 创建一个父表格组件和一个子表格组件,通过 props 将数据传递给子表格组件。父表格的每…

css好看的表格制作

css好看的表格制作

CSS表格美化方法 边框与间距优化 使用border-collapse合并边框,避免双线边框。设置padding增加单元格内边距,提升可读性。 table { border-collapse:…

css表格制作

css表格制作

CSS 表格制作基础 使用 CSS 可以创建美观且响应式的表格。以下是一些基本方法: table 元素用于定义表格,tr 定义行,td 定义单元格,th 定义表头单元格。 <table&…