vue.js 实现分页
Vue.js 分页实现方法
基础分页组件实现
创建一个基础的Pagination组件,接收总页数、当前页等参数,通过v-for渲染页码按钮:
<template>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<button
v-for="page in pages"
:key="page"
@click="goToPage(page)"
:class="{ active: page === currentPage }"
>
{{ page }}
</button>
<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>
在父组件中使用分页
父组件中维护数据列表和分页状态,通过v-model或事件绑定与分页组件交互:
<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组件的computed中替换pages计算属性
pages() {
const current = this.currentPage
const last = this.totalPages
const delta = 2
const left = current - delta
const right = current + delta + 1
const range = []
const rangeWithDots = []
let l
for (let i = 1; i <= last; i++) {
if (i === 1 || i === last || (i >= left && i < right)) {
range.push(i)
}
}
range.forEach(i => {
if (l) {
if (i - l === 2) {
rangeWithDots.push(l + 1)
} else if (i - l !== 1) {
rangeWithDots.push('...')
}
}
rangeWithDots.push(i)
l = i
})
return rangeWithDots
}
与API集成
结合axios等HTTP客户端实现服务器端分页:
methods: {
async fetchData(page = 1) {
try {
const response = await axios.get('/api/items', {
params: {
page,
per_page: this.itemsPerPage
}
})
this.items = response.data.data
this.totalItems = response.data.total
} catch (error) {
console.error(error)
}
},
handlePageChange(page) {
this.currentPage = page
this.fetchData(page)
}
},
created() {
this.fetchData()
}
使用第三方库
对于快速实现,可以考虑使用现成的分页库如vuejs-paginate:
npm install vuejs-paginate
<template>
<paginate
:page-count="totalPages"
:click-handler="handlePageChange"
:prev-text="'<'"
:next-text="'>'"
:container-class="'pagination'"
:page-class="'page-item'"
/>
</template>
<script>
import Paginate from 'vuejs-paginate'
export default {
components: { Paginate }
}
</script>
这些方法覆盖了从基础到高级的Vue.js分页实现方案,可以根据项目需求选择合适的实现方式。







