vue实现分页数据
Vue 分页实现方法
基础分页组件实现
创建一个分页组件Pagination.vue,包含页码按钮和导航逻辑:
<template>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<span v-for="page in pages" :key="page">
<button
@click="changePage(page)"
:class="{ active: currentPage === page }"
>{{ page }}</button>
</span>
<button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
</div>
</template>
<script>
export default {
props: {
totalItems: Number,
itemsPerPage: Number,
currentPage: Number
},
computed: {
totalPages() {
return Math.ceil(this.totalItems / this.itemsPerPage)
},
pages() {
const range = []
for (let i = 1; i <= this.totalPages; i++) {
range.push(i)
}
return range
}
},
methods: {
changePage(page) {
this.$emit('page-changed', page)
},
prevPage() {
if (this.currentPage > 1) {
this.$emit('page-changed', this.currentPage - 1)
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.$emit('page-changed', this.currentPage + 1)
}
}
}
}
</script>
<style>
.pagination button {
margin: 0 5px;
}
.pagination button.active {
background-color: #42b983;
color: white;
}
</style>
在父组件中使用分页
<template>
<div>
<ul>
<li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
</ul>
<Pagination
:total-items="data.length"
:items-per-page="itemsPerPage"
:current-page="currentPage"
@page-changed="changePage"
/>
</div>
</template>
<script>
import Pagination from './Pagination.vue'
export default {
components: { Pagination },
data() {
return {
data: [], // 从API获取的全部数据
itemsPerPage: 10,
currentPage: 1
}
},
computed: {
paginatedData() {
const start = (this.currentPage - 1) * this.itemsPerPage
const end = start + this.itemsPerPage
return this.data.slice(start, end)
}
},
methods: {
changePage(page) {
this.currentPage = page
},
async fetchData() {
// API数据获取逻辑
this.data = await api.getItems()
}
},
created() {
this.fetchData()
}
}
</script>
服务器端分页实现
对于大数据量,建议使用服务器端分页:

<script>
export default {
data() {
return {
paginatedData: [],
totalItems: 0,
itemsPerPage: 10,
currentPage: 1
}
},
methods: {
async fetchPaginatedData() {
const response = await api.getItems({
page: this.currentPage,
limit: this.itemsPerPage
})
this.paginatedData = response.data
this.totalItems = response.total
},
changePage(page) {
this.currentPage = page
this.fetchPaginatedData()
}
},
created() {
this.fetchPaginatedData()
}
}
</script>
高级分页功能扩展
实现显示页码范围限制和省略号:
// 在Pagination组件的computed中替换pages方法
pages() {
const range = []
const visiblePages = 5 // 可见页码数
let start = Math.max(1, this.currentPage - Math.floor(visiblePages/2))
let end = Math.min(this.totalPages, start + visiblePages - 1)
if (end - start + 1 < visiblePages) {
start = Math.max(1, end - visiblePages + 1)
}
if (start > 1) {
range.push(1)
if (start > 2) range.push('...')
}
for (let i = start; i <= end; i++) {
range.push(i)
}
if (end < this.totalPages) {
if (end < this.totalPages - 1) range.push('...')
range.push(this.totalPages)
}
return range
}
使用第三方库
对于快速实现,可以使用现成的分页库如vuejs-paginate:

安装:
npm install vuejs-paginate
使用:
<template>
<paginate
:page-count="totalPages"
:click-handler="changePage"
:prev-text="'<'"
:next-text="'>'"
:container-class="'pagination'"
:page-class="'page-item'"
/>
</template>
<script>
import Paginate from 'vuejs-paginate'
export default {
components: { Paginate },
// 其他逻辑相同
}
</script>






