vue实现分页功能例子
vue实现分页功能例子
基础分页组件实现
创建一个简单的分页组件,包含页码按钮和导航功能。以下是一个基本的分页组件代码示例:
<template>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<span v-for="page in pages" :key="page">
<button
@click="changePage(page)"
:class="{ active: currentPage === page }"
>
{{ 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
}
},
data() {
return {
currentPage: 1
}
},
computed: {
totalPages() {
return Math.ceil(this.totalItems / this.itemsPerPage)
},
pages() {
const pages = []
for (let i = 1; i <= this.totalPages; i++) {
pages.push(i)
}
return pages
}
},
methods: {
changePage(page) {
this.currentPage = page
this.$emit('page-changed', page)
},
prevPage() {
if (this.currentPage > 1) {
this.changePage(this.currentPage - 1)
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.changePage(this.currentPage + 1)
}
}
}
}
</script>
<style>
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination button {
margin: 0 5px;
padding: 5px 10px;
}
.pagination button.active {
background-color: #42b983;
color: white;
}
</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"
@page-changed="handlePageChange"
/>
</div>
</template>
<script>
import Pagination from './Pagination.vue'
export default {
components: {
Pagination
},
data() {
return {
items: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
// 更多数据...
],
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>
高级分页功能
对于大量数据,可以添加页码限制和省略号显示:
// 在分页组件的computed中修改pages计算属性
pages() {
const range = 2 // 显示当前页前后各2页
const pages = []
let start = Math.max(1, this.currentPage - range)
let end = Math.min(this.totalPages, this.currentPage + range)
if (start > 1) {
pages.push(1)
if (start > 2) {
pages.push('...')
}
}
for (let i = start; i <= end; i++) {
pages.push(i)
}
if (end < this.totalPages) {
if (end < this.totalPages - 1) {
pages.push('...')
}
pages.push(this.totalPages)
}
return pages
}
服务器端分页
与API配合使用时,可以这样处理:
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)
}
}
使用第三方库
如果需要更复杂的功能,可以考虑使用现成的分页库,如vue-paginate:
npm install vue-paginate
<template>
<paginate
v-model="currentPage"
:page-count="totalPages"
:click-handler="handlePageChange"
:prev-text="'Prev'"
:next-text="'Next'"
:container-class="'pagination'"
:page-class="'page-item'"
>
</paginate>
</template>
<script>
import Paginate from 'vue-paginate'
export default {
components: {
Paginate
},
// 其他代码...
}
</script>
这些示例展示了从基础到高级的Vue分页实现方法,可以根据项目需求选择合适的方案。







