vue 分页的实现
分页组件实现
使用Vue实现分页功能通常需要结合后端API和前端组件。以下是一种常见的实现方式:
模板部分
<template>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<span v-for="page in pageRange" :key="page">
<button
@click="goToPage(page)"
:class="{ active: page === currentPage }"
>
{{ page }}
</button>
</span>
<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
},
maxVisibleButtons: {
type: Number,
default: 5
}
},
computed: {
totalPages() {
return Math.ceil(this.totalItems / this.itemsPerPage)
},
pageRange() {
const range = []
const half = Math.floor(this.maxVisibleButtons / 2)
let start = Math.max(1, this.currentPage - half)
let end = Math.min(this.totalPages, start + this.maxVisibleButtons - 1)
if (end - start + 1 < this.maxVisibleButtons) {
start = Math.max(1, end - this.maxVisibleButtons + 1)
}
for (let i = start; i <= end; 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 scoped>
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination button {
margin: 0 5px;
padding: 5px 10px;
border: 1px solid #ddd;
background: #fff;
cursor: pointer;
}
.pagination button.active {
background: #42b983;
color: white;
border-color: #42b983;
}
.pagination button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
</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="perPage"
:current-page="currentPage"
@page-changed="changePage"
/>
</div>
</template>
<script>
import Pagination from './Pagination.vue'
export default {
components: { Pagination },
data() {
return {
data: [], // 从API获取的数据
perPage: 10,
currentPage: 1
}
},
computed: {
paginatedData() {
const start = (this.currentPage - 1) * this.perPage
const end = start + this.perPage
return this.data.slice(start, end)
}
},
methods: {
changePage(page) {
this.currentPage = page
// 可以在这里调用API获取新页面的数据
}
}
}
</script>
与后端API配合
当数据量很大时,通常需要后端分页:
methods: {
async fetchData(page = 1) {
try {
const response = await axios.get('/api/items', {
params: {
page,
per_page: this.perPage
}
})
this.data = response.data.items
this.totalItems = response.data.total
} catch (error) {
console.error(error)
}
},
changePage(page) {
this.currentPage = page
this.fetchData(page)
}
}
分页功能优化
显示总页数和当前页信息:
<div class="pagination-info">
第 {{ currentPage }} 页 / 共 {{ totalPages }} 页 ({{ totalItems }} 条记录)
</div>
添加跳转输入框:
<input
type="number"
v-model.number="inputPage"
min="1"
:max="totalPages"
@keyup.enter="goToInputPage"
>
<button @click="goToInputPage">跳转</button>
data() {
return {
inputPage: 1
}
},
methods: {
goToInputPage() {
const page = Math.max(1, Math.min(this.inputPage, this.totalPages))
this.$emit('page-changed', page)
this.inputPage = page
}
}






