vue实现分页组件
Vue 分页组件实现
基础分页组件结构
创建一个名为 Pagination.vue 的文件,包含以下基础模板和逻辑:
<template>
<div class="pagination">
<button
@click="changePage(currentPage - 1)"
:disabled="currentPage === 1"
>
上一页
</button>
<span
v-for="page in pages"
:key="page"
@click="changePage(page)"
:class="{ active: currentPage === page }"
>
{{ page }}
</span>
<button
@click="changePage(currentPage + 1)"
: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: {
changePage(page) {
if (page >= 1 && page <= this.totalPages) {
this.$emit('page-changed', page)
}
}
}
}
</script>
<style scoped>
.pagination {
display: flex;
gap: 8px;
}
.active {
font-weight: bold;
color: blue;
}
</style>
优化分页显示逻辑
当页数较多时,可以只显示部分页码,添加以下计算属性替换原来的 pages:
pages() {
const range = []
const maxVisible = 5 // 最多显示5个页码
let start = 1
let end = this.totalPages
if (this.totalPages > maxVisible) {
start = Math.max(this.currentPage - 2, 1)
end = Math.min(this.currentPage + 2, this.totalPages)
if (this.currentPage <= 3) {
end = maxVisible
} else if (this.currentPage >= this.totalPages - 2) {
start = this.totalPages - maxVisible + 1
}
}
for (let i = start; i <= end; i++) {
range.push(i)
}
return range
}
在父组件中使用
<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>
添加省略号功能
修改分页组件模板,添加省略号显示:
<template>
<div class="pagination">
<button @click="changePage(1)" :disabled="currentPage === 1">首页</button>
<button @click="changePage(currentPage - 1)" :disabled="currentPage === 1">上一页</button>
<span v-if="showStartEllipsis">...</span>
<span
v-for="page in pages"
:key="page"
@click="changePage(page)"
:class="{ active: currentPage === page }"
>
{{ page }}
</span>
<span v-if="showEndEllipsis">...</span>
<button @click="changePage(currentPage + 1)" :disabled="currentPage === totalPages">下一页</button>
<button @click="changePage(totalPages)" :disabled="currentPage === totalPages">末页</button>
</div>
</template>
<script>
export default {
// ...其他代码不变...
computed: {
showStartEllipsis() {
return this.pages.length > 0 && this.pages[0] > 1
},
showEndEllipsis() {
return this.pages.length > 0 &&
this.pages[this.pages.length - 1] < this.totalPages
}
}
}
</script>
响应式设计考虑
添加媒体查询适应移动设备:
@media (max-width: 600px) {
.pagination {
flex-wrap: wrap;
justify-content: center;
}
.pagination button, .pagination span {
margin: 4px;
padding: 4px 8px;
}
}






