当前位置:首页 > VUE

vue表格分页如何实现

2026-02-21 15:04:03VUE

实现Vue表格分页的方法

在Vue中实现表格分页通常需要结合前端分页逻辑和后端API分页请求。以下是两种常见的实现方式:

前端分页实现

适用于数据量较小的情况,所有数据一次性加载到前端后通过计算属性分页。

模板部分

<template>
  <div>
    <table>
      <tr v-for="item in paginatedData" :key="item.id">
        <td>{{ item.name }}</td>
        <!-- 其他列 -->
      </tr>
    </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
    }
  },
  computed: {
    pageCount() {
      return Math.ceil(this.tableData.length / 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++
    }
  }
}
</script>

后端分页实现

适用于大数据量场景,每次只请求当前页的数据。

模板部分

<template>
  <div>
    <table>
      <tr v-for="item in tableData" :key="item.id">
        <td>{{ item.name }}</td>
        <!-- 其他列 -->
      </tr>
    </table>
    <div class="pagination">
      <button @click="fetchData(currentPage - 1)" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="fetchData(currentPage + 1)" :disabled="!hasNextPage">下一页</button>
    </div>
  </div>
</template>

脚本部分

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      totalItems: 0
    }
  },
  computed: {
    hasNextPage() {
      return this.currentPage * this.pageSize < this.totalItems
    }
  },
  methods: {
    async fetchData(page) {
      try {
        const response = await api.get('/data', {
          params: {
            page,
            size: this.pageSize
          }
        })
        this.tableData = response.data.items
        this.totalItems = response.data.total
        this.currentPage = page
      } catch (error) {
        console.error(error)
      }
    }
  },
  created() {
    this.fetchData(1)
  }
}
</script>

使用第三方组件库

流行的UI库如Element UI、Ant Design Vue等提供了现成的分页组件:

Element UI示例

<template>
  <el-table :data="tableData">
    <el-table-column prop="name" label="姓名"></el-table-column>
    <!-- 其他列 -->
  </el-table>
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="total">
  </el-pagination>
</template>

<script>
export default {
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    }
  }
}
</script>

分页样式优化

可以添加CSS改善分页控件外观:

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
}

以上方法可根据实际项目需求选择使用,大数据量推荐后端分页,小数据量前端分页更简单高效。

vue表格分页如何实现

分享给朋友:

相关文章

vue 实现分页

vue 实现分页

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

vue如何实现重新实现主题

vue如何实现重新实现主题

Vue 主题切换的实现方法 使用 CSS 变量动态切换主题 定义主题相关的 CSS 变量,通过修改这些变量实现主题切换。在根元素(如 :root)中定义默认主题的变量,在特定类名下定义其他主题的变量。…

vue如何实现拖动

vue如何实现拖动

Vue 实现拖动的常用方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性和相关事件实现拖动功能。 <template>…

vue 分页的实现

vue 分页的实现

分页组件实现 使用Vue实现分页功能通常需要结合后端API和前端组件。以下是一种常见的实现方式: 模板部分 <template> <div class="pagina…

jquery分页

jquery分页

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

h5页面如何实现

h5页面如何实现

实现H5页面的方法 H5页面是基于HTML5技术的网页,通常用于移动端和响应式设计。以下是实现H5页面的关键步骤和技术要点。 基础结构 使用HTML5的DOCTYPE声明作为页面的起始。HTML5简…