vue 实现简单分页
Vue 实现简单分页的方法
基础分页组件实现
创建一个基础分页组件,包含页码按钮和翻页功能。以下是一个基于 Vue 3 的示例:
<template>
<div class="pagination">
<button
@click="prevPage"
:disabled="currentPage === 1"
>
上一页
</button>
<span
v-for="page in pages"
:key="page"
@click="changePage(page)"
:class="{ active: currentPage === page }"
>
{{ page }}
</span>
<button
@click="nextPage"
:disabled="currentPage === totalPages"
>
下一页
</button>
</div>
</template>
<script>
export default {
props: {
totalItems: Number,
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) {
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 {
display: flex;
gap: 8px;
}
.pagination span {
cursor: pointer;
padding: 4px 8px;
}
.pagination span.active {
font-weight: bold;
color: blue;
}
</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>
优化分页显示
对于大量数据,可以优化页码显示,只显示当前页附近的页码:
// 在Pagination组件的computed中替换pages计算属性
pages() {
const range = []
const maxVisible = 5 // 最多显示5个页码
let start = 1
let end = this.totalPages
if (this.totalPages > maxVisible) {
start = Math.max(1, this.currentPage - 2)
end = Math.min(this.totalPages, this.currentPage + 2)
if (this.currentPage <= 3) {
end = maxVisible
}
if (this.currentPage >= this.totalPages - 2) {
start = this.totalPages - maxVisible + 1
}
}
for (let i = start; i <= end; i++) {
range.push(i)
}
return range
}
与API集成
当数据来自后端API时,可以在分页变化时发送请求:
methods: {
handlePageChange(page) {
this.currentPage = page
this.fetchData()
},
async fetchData() {
const response = await axios.get('/api/items', {
params: {
page: this.currentPage,
per_page: this.itemsPerPage
}
})
this.items = response.data.items
this.totalItems = response.data.total_count
}
}
样式优化建议
为分页组件添加更多样式选项,使其更美观:
.pagination {
display: flex;
justify-content: center;
gap: 4px;
margin-top: 20px;
}
.pagination button {
padding: 6px 12px;
border: 1px solid #ddd;
background: white;
cursor: pointer;
}
.pagination button:disabled {
cursor: not-allowed;
opacity: 0.5;
}
.pagination span {
padding: 6px 12px;
border: 1px solid #ddd;
cursor: pointer;
}
.pagination span.active {
background: #007bff;
color: white;
border-color: #007bff;
}
这些方法提供了从基础到进阶的Vue分页实现方案,可以根据项目需求选择合适的实现方式。







