列表分页
unicloud-db组件简化了列表分页的写法,只需简单的配置每页需要多少数据(默认是20条),不管是数据库的分页查询还是前端的列表分页展示,都自动封装了。
列表分页有2种模式,一种是手机上常见的拉到底部加载下一页,另一种是pc常见的底部列出页码,点击页码跳转页面。
列表分页模式1:拉到底部加载下一页。此时下一页的查询结果会追加合并到data里,列表一直在增长。
下面的示例代码没有使用uList组件,实际开发时建议使用uList组件来避免长列表的性能问题。
<template>
<view class="content">
<unicloud-db ref="udb" v-slot:default="{data, pagination, loading, error, options}"
:options="options"
collection="table1"
orderby="createTime desc"
field="name,subType,createTime"
:getone="false"
:action="action"
:where="where"
@load="onqueryload" @error="onqueryerror">
<view v-if="error" class="error">{{error.message}}</view>
<view v-else class="list">
<view v-for="(item, index) in data" :key="index" class="list-item">
{{item.name}}
<!-- 使用日期格式化组件,详情见插件 https://ext.dcloud.net.cn/search?q=date-format -->
<uni-dateformat :date="item.createTime" />
</view>
</view>
<view v-if="loading" class="loading">加载中...</view>
</unicloud-db>
</view>
</template>
<script>
export default {
data() {
return {
options: {}, // 插槽不能访问外面的数据,通过此参数传递, 不支持传递函数
action: '',
where: {} // 类型为对象或字符串
}
},
onPullDownRefresh() { //下拉刷新
this.$refs.udb.loadData({
clear: true //可选参数,是否清空数据
}, () => {
uni.stopPullDownRefresh()
})
},
onReachBottom() { //滚动到底翻页
this.$refs.udb.loadMore()
},
methods: {
onqueryload(data, ended) {
// 可在此处预处理数据,然后再渲染界面
},
onqueryerror(e) {
// 加载数据失败
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
background-color: #f0f0f0;
}
.list-item {
background-color: #fff;
margin-bottom: 1px;
padding: 30px 15px;
}
.loading {
padding: 20px;
text-align: center;
}
.error {
color: #DD524D;
}
</style>列表分页模式2:使用分页控件,点击第二页则只显示第二页数据,第一页数据清空。data会重置为下一页的查询结果,上一页数据丢弃
<template>
<view class="content">
<unicloud-db ref="udb" v-slot:default="{data, pagination, loading, error, options}"
:options="options"
collection="table1"
orderby="createTime desc"
field="name,subType,createTime"
:getcount="true"
@load="onqueryload" @error="onqueryerror">
<view>{{pagination}}</view>
<view v-if="error" class="error">{{error.errMsg}}</view>
<view v-else class="list">
<view v-for="(item, index) in data" :key="index" class="list-item">
{{item.name}}
<!-- 使用日期格式化组件,详情见插件 https://ext.dcloud.net.cn/search?q=date-format -->
<uni-dateformat :date="item.createTime" />
</view>
</view>
<view class="loading" v-if="loading">加载中...</view>
<!-- 分页组件 -->
<uni-pagination show-icon :page-size="pagination.size" total="pagination.count" @change="onpagination" />
</unicloud-db>
</view>
</template>
<script>
export default {
data() {
return {
options: {}, // 插槽不能访问外面的数据,通过此参数传递, 不支持传递函数
}
},
onPullDownRefresh() { //下拉刷新(一般此场景下不使用下拉刷新)
this.$refs.udb.loadData({
clear: true
}, () => {
uni.stopPullDownRefresh()
})
},
methods: {
onqueryload(data, ended) {
// 可在此处预处理数据,然后再渲染界面
},
onqueryerror(e) {
// 加载数据失败
},
onpagination(e) {
this.$refs.udb.loadData({
current: e.current
})
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
background-color: #f0f0f0;
}
.list-item {
background-color: #fff;
margin-bottom: 1px;
padding: 30px 15px;
}
.loading {
padding: 20px;
text-align: center;
}
.error {
color: #DD524D;
}
</style>转载:https://uniapp.dcloud.io/uniCloud/unicloud-db.html#page
|
|---|