uniapp写一个分页器能实现列表分页
时间: 2023-07-18 11:46:34 浏览: 55
可以的,下面是一个简单的uniapp分页器实现:
1.在页面中定义一个列表组件和分页器组件:
```
<template>
<view>
<scroll-view scroll-y="true">
<view v-for="(item, index) in list" :key="index">{{item}}</view>
</scroll-view>
<view class="pagination">
<view class="prev" @click="prev">上一页</view>
<view class="page" v-for="(item, index) in pages" :key="index" :class="{'active': item === currentPage}" @click="goToPage(item)">{{item}}</view>
<view class="next" @click="next">下一页</view>
</view>
</view>
</template>
```
2.在页面的script中定义相关数据和方法:
```
<script>
export default {
data() {
return {
list: [], // 列表数据
pageSize: 10, // 每页数据量
currentPage: 1, // 当前页码
totalPage: 0, // 总页数
pages: [] // 分页器页码列表
}
},
mounted() {
this.getList()
},
methods: {
// 获取列表数据
getList() {
// 根据当前页码和每页数据量获取对应的数据
// 可以通过Ajax请求后台接口获取数据
// 此处省略Ajax请求过程
const start = (this.currentPage - 1) * this.pageSize
const end = start + this.pageSize
this.list = this.mockData.slice(start, end)
// 计算总页数
this.totalPage = Math.ceil(this.mockData.length / this.pageSize)
// 生成分页器页码列表
this.pages = []
for (let i = 1; i <= this.totalPage; i++) {
this.pages.push(i)
}
},
// 上一页
prev() {
if (this.currentPage > 1) {
this.currentPage--
this.getList()
}
},
// 下一页
next() {
if (this.currentPage < this.totalPage) {
this.currentPage++
this.getList()
}
},
// 跳转到指定页码
goToPage(page) {
if (page !== this.currentPage) {
this.currentPage = page
this.getList()
}
}
}
}
</script>
```
3.在页面的style中定义分页器样式:
```
<style>
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination .prev,
.pagination .next,
.pagination .page {
display: inline-block;
padding: 5px 10px;
margin: 0 5px;
border: 1px solid #ccc;
cursor: pointer;
}
.pagination .prev:hover,
.pagination .next:hover,
.pagination .page:hover {
background-color: #eee;
}
.pagination .active {
background-color: #ccc;
color: #fff;
}
</style>
```
这样就完成了一个简单的uniapp分页器实现,可以根据实际需求进行修改和优化。