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="goToPage(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: {
prevPage() {
this.$emit('page-changed', this.currentPage - 1)
},
nextPage() {
this.$emit('page-changed', this.currentPage + 1)
},
goToPage(page) {
this.$emit('page-changed', page)
}
}
}
</script>
<style>
.pagination button {
margin: 0 5px;
}
.pagination button.active {
background-color: #42b983;
color: white;
}
</style>
在父组件中使用分页
实现数据分页逻辑,监听分页组件事件:
<template>
<div>
<ul>
<li v-for="item in paginatedItems" :key="item.id">{{ item.name }}</li>
</ul>
<Pagination
:total-items="items.length"
:items-per-page="itemsPerPage"
:current-page="currentPage"
@page-changed="handlePageChange"
/>
</div>
</template>
<script>
import Pagination from './Pagination.vue'
export default {
components: { Pagination },
data() {
return {
items: [], // 从API获取或本地数据
itemsPerPage: 10,
currentPage: 1
}
},
computed: {
paginatedItems() {
const start = (this.currentPage - 1) * this.itemsPerPage
const end = start + this.itemsPerPage
return this.items.slice(start, end)
}
},
methods: {
handlePageChange(page) {
this.currentPage = page
}
}
}
</script>
高级分页功能扩展
添加页码省略功能,避免过多页码按钮:
// 在Pagination组件的pages计算属性中修改
pages() {
const current = this.currentPage
const total = this.totalPages
const range = []
// 总是显示第一页
range.push(1)
// 当前页前后各显示2页
for (let i = Math.max(2, current - 2); i <= Math.min(current + 2, total - 1); i++) {
range.push(i)
}
// 总是显示最后一页
if (total > 1) {
range.push(total)
}
// 添加省略号逻辑
const filtered = []
range.forEach((page, i) => {
if (i > 0 && page - range[i-1] > 1) {
filtered.push('...')
}
filtered.push(page)
})
return filtered
}
与API结合的分页实现
当数据来自后端API时,修改获取数据方法:
methods: {
async fetchData(page = 1) {
const response = await axios.get('/api/items', {
params: {
page,
per_page: this.itemsPerPage
}
})
this.items = response.data.items
this.totalItems = response.data.total_count
},
handlePageChange(page) {
this.currentPage = page
this.fetchData(page)
}
},
created() {
this.fetchData()
}
分页样式优化
添加更美观的样式方案:
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination button {
padding: 5px 10px;
margin: 0 2px;
border: 1px solid #ddd;
background: white;
cursor: pointer;
border-radius: 3px;
}
.pagination button:hover:not(:disabled) {
background: #f5f5f5;
}
.pagination button.active {
background: #42b983;
color: white;
border-color: #42b983;
}
.pagination button:disabled {
opacity: 0.5;
cursor: not-allowed;
}






