用vue实现分页效果
使用Vue实现分页效果
基础分页组件实现
创建一个简单的分页组件,包含页码按钮、上一页和下一页按钮。以下是一个基本实现:
<template>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<button
v-for="page in pages"
:key="page"
@click="goToPage(page)"
:class="{ active: currentPage === page }"
>
{{ page }}
</button>
<button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
</div>
</template>
<script>
export default {
props: {
totalItems: {
type: Number,
required: true
},
itemsPerPage: {
type: Number,
default: 10
},
currentPage: {
type: Number,
default: 1
}
},
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() {
if (this.currentPage > 1) {
this.$emit('page-changed', this.currentPage - 1)
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.$emit('page-changed', this.currentPage + 1)
}
},
goToPage(page) {
this.$emit('page-changed', page)
}
}
}
</script>
<style>
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination button {
margin: 0 5px;
padding: 5px 10px;
cursor: pointer;
}
.pagination button.active {
background-color: #42b983;
color: white;
}
.pagination button:disabled {
cursor: not-allowed;
opacity: 0.5;
}
</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: 5,
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>
高级分页功能
对于大型数据集,可以添加以下改进:
限制显示的页码数量
修改分页组件的pages计算属性:

pages() {
const range = []
const maxVisible = 5 // 最多显示5个页码
let start = 1
if (this.totalPages > maxVisible) {
start = Math.min(
Math.max(1, this.currentPage - Math.floor(maxVisible / 2)),
this.totalPages - maxVisible + 1
)
}
const end = Math.min(start + maxVisible - 1, this.totalPages)
for (let i = start; i <= end; i++) {
range.push(i)
}
return range
}
添加省略号表示更多页码
在模板中添加条件渲染:
<button v-if="showStartEllipsis" disabled>...</button>
<button
v-for="page in visiblePages"
:key="page"
@click="goToPage(page)"
:class="{ active: currentPage === page }"
>
{{ page }}
</button>
<button v-if="showEndEllipsis" disabled>...</button>
添加跳转到指定页功能

在分页组件中添加输入框:
<input
type="number"
v-model.number="inputPage"
min="1"
:max="totalPages"
@keyup.enter="goToInputPage"
>
<button @click="goToInputPage">跳转</button>
服务器端分页
当处理大量数据时,通常使用服务器端分页:
methods: {
async fetchData(page) {
try {
const response = await axios.get('/api/items', {
params: {
page,
per_page: this.itemsPerPage
}
})
this.items = response.data.items
this.totalItems = response.data.total
} catch (error) {
console.error(error)
}
},
handlePageChange(page) {
this.currentPage = page
this.fetchData(page)
}
}
使用第三方库
可以考虑使用现成的分页库如vuejs-paginate:
npm install vuejs-paginate
使用示例:
import Paginate from 'vuejs-paginate'
export default {
components: {
Paginate
},
methods: {
handlePageChange(page) {
this.currentPage = page
}
}
}
<paginate
:page-count="totalPages"
:click-handler="handlePageChange"
:prev-text="'上一页'"
:next-text="'下一页'"
:container-class="'pagination'"
:page-class="'page-item'"
>
</paginate>
这些方法提供了从基础到高级的分页实现方案,可以根据项目需求选择合适的实现方式。






