当前位置:首页 > VUE

vue实现分页表

2026-01-17 12:42:14VUE

Vue 实现分页表格

在 Vue 中实现分页表格通常需要结合表格组件和分页组件,以下是常见的实现方法:

使用 Element UI 实现分页表格

安装 Element UI:

npm install element-ui

在 main.js 中引入:

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

组件实现:

<template>
  <div>
    <el-table :data="currentTableData" border>
      <el-table-column prop="date" label="日期"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="address" 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="tableData.length">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [], // 全部数据
      currentPage: 1,
      pageSize: 10,
      currentTableData: [] // 当前页数据
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.updateCurrentTableData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.updateCurrentTableData()
    },
    updateCurrentTableData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      this.currentTableData = this.tableData.slice(start, end)
    }
  },
  mounted() {
    // 模拟获取数据
    this.tableData = Array.from({length: 100}, (_, i) => ({
      date: `2023-01-${i+1}`,
      name: `用户${i+1}`,
      address: `地址${i+1}`
    }))
    this.updateCurrentTableData()
  }
}
</script>

使用原生 Vue 实现分页表格

如果不使用 UI 框架,可以原生实现:

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>日期</th>
          <th>姓名</th>
          <th>地址</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in currentTableData" :key="item.id">
          <td>{{ item.date }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.address }}</td>
        </tr>
      </tbody>
    </table>

    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    currentTableData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.tableData.slice(start, end)
    },
    totalPages() {
      return Math.ceil(this.tableData.length / this.itemsPerPage)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++
    }
  },
  mounted() {
    // 模拟获取数据
    this.tableData = Array.from({length: 100}, (_, i) => ({
      id: i+1,
      date: `2023-01-${i+1}`,
      name: `用户${i+1}`,
      address: `地址${i+1}`
    }))
  }
}
</script>

<style>
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: 20px;
}
</style>

使用 Vue 3 Composition API 实现

Vue 3 可以使用 Composition API 更简洁地实现:

<template>
  <!-- 表格和分页组件同上 -->
</template>

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

const tableData = ref(Array.from({length: 100}, (_, i) => ({
  id: i+1,
  date: `2023-01-${i+1}`,
  name: `用户${i+1}`,
  address: `地址${i+1}`
})))

const currentPage = ref(1)
const itemsPerPage = ref(10)

const currentTableData = computed(() => {
  const start = (currentPage.value - 1) * itemsPerPage.value
  const end = start + itemsPerPage.value
  return tableData.value.slice(start, end)
})

const totalPages = computed(() => 
  Math.ceil(tableData.value.length / itemsPerPage.value)
)

function prevPage() {
  if (currentPage.value > 1) currentPage.value--
}

function nextPage() {
  if (currentPage.value < totalPages.value) currentPage.value++
}
</script>

关键点总结

分页实现的核心逻辑是计算当前页应该显示的数据片段。通常需要维护以下变量:

  • 全部数据数组
  • 当前页码
  • 每页显示数量
  • 总页数(计算属性)

分页组件需要处理的主要事件:

  • 页码改变事件
  • 每页数量改变事件

对于大数据量,建议在后端实现分页,前端只请求当前页的数据,而不是一次性获取所有数据。

vue实现分页表

标签: 分页vue
分享给朋友:

相关文章

vue实现单词反转

vue实现单词反转

实现思路 在Vue中实现单词反转功能,可以通过多种方式完成。核心思路是利用JavaScript的字符串处理方法,将输入的单词或句子拆分为数组,反转数组后再拼接成字符串。 方法一:使用计算属性 通过…

vue如何实现注册

vue如何实现注册

实现用户注册功能 在Vue中实现注册功能通常需要结合前端表单和后端API交互。以下是完整的实现方案: 创建注册表单组件 注册表单需要包含用户名、邮箱、密码等基本字段,并进行表单验证: <…

课程表vue实现

课程表vue实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ {…

vue自己实现下拉导航

vue自己实现下拉导航

实现下拉导航的基本思路 在Vue中实现下拉导航,可以通过结合Vue的响应式数据和事件绑定特性来完成。主要思路是利用v-show或v-if控制下拉菜单的显示与隐藏,并通过鼠标事件或点击事件触发状态变化。…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交…

vue实现视窗

vue实现视窗

Vue 实现视窗功能 在 Vue 中实现视窗功能通常涉及监听浏览器窗口大小变化、响应式布局或创建自定义弹窗组件。以下是几种常见实现方式: 监听浏览器窗口大小变化 使用 Vue 的 mounted 和…