vue普通表格实现分页
Vue 表格分页实现方法
在 Vue 中实现表格分页可以通过多种方式完成,以下是几种常见的方法:
使用 Element UI 分页组件
安装 Element UI 后可以直接使用其分页组件:

<template>
<div>
<el-table :data="currentTableData">
<!-- 表格列定义 -->
</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
}
},
computed: {
currentTableData() {
return this.tableData.slice(
(this.currentPage - 1) * this.pageSize,
this.currentPage * this.pageSize
)
}
},
methods: {
handleSizeChange(val) {
this.pageSize = val
},
handleCurrentChange(val) {
this.currentPage = val
}
},
created() {
// 获取数据
fetchData().then(data => {
this.tableData = data
this.total = data.length
})
}
}
</script>
手动实现分页功能
如果不使用 UI 库,可以手动实现分页:

<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 {
allData: [],
currentPage: 1,
itemsPerPage: 10
}
},
computed: {
pageCount() {
return Math.ceil(this.allData.length / this.itemsPerPage)
},
paginatedData() {
const start = (this.currentPage - 1) * this.itemsPerPage
const end = start + this.itemsPerPage
return this.allData.slice(start, end)
}
},
methods: {
nextPage() {
if (this.currentPage < this.pageCount) {
this.currentPage++
}
},
prevPage() {
if (this.currentPage > 1) {
this.currentPage--
}
}
}
}
</script>
使用 Vue 插件
可以使用专门的分页插件如 vuejs-paginate:
// 安装插件
npm install vuejs-paginate
// 使用示例
<template>
<div>
<table>
<!-- 表格内容 -->
</table>
<paginate
v-model="currentPage"
:page-count="totalPages"
:click-handler="changePage"
:prev-text="'Prev'"
:next-text="'Next'"
:container-class="'pagination'">
</paginate>
</div>
</template>
<script>
import Paginate from 'vuejs-paginate'
export default {
components: {
Paginate
},
data() {
return {
currentPage: 1,
itemsPerPage: 10,
allItems: []
}
},
computed: {
totalPages() {
return Math.ceil(this.allItems.length / this.itemsPerPage)
}
},
methods: {
changePage(pageNum) {
this.currentPage = pageNum
}
}
}
</script>
后端分页处理
对于大数据量,建议使用后端分页:
<template>
<div>
<table>
<!-- 表格内容 -->
</table>
<div class="pagination">
<!-- 分页控件 -->
</div>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [],
currentPage: 1,
pageSize: 10,
total: 0
}
},
methods: {
fetchData() {
const params = {
page: this.currentPage,
size: this.pageSize
}
axios.get('/api/data', { params })
.then(response => {
this.tableData = response.data.items
this.total = response.data.total
})
},
handlePageChange(page) {
this.currentPage = page
this.fetchData()
}
},
created() {
this.fetchData()
}
}
</script>
选择哪种方法取决于项目需求和技术栈。UI 库提供现成解决方案,手动实现更灵活,后端分页适合大数据量场景。






