当前位置:网站首页>uni-app 数据上拉加载更多功能
uni-app 数据上拉加载更多功能
2022-08-10 19:01:00 【船长在船上】
实现上拉加载更多
- 打开项目根目录中的
pages.json
配置文件,为subPackages
分包中的商品goods_list
页面配置上拉触底的距离:
"subPackages": [
{
"root": "subpkg",
"pages": [
{
"path": "goods_detail/goods_detail",
"style": {}
},
{
"path": "goods_list/goods_list",
"style": {
"onReachBottomDistance": 150
}
},
{
"path": "search/search",
"style": {}
}
]
}
]
- 在
goods_list
页面中,和methods
节点平级,声明onReachBottom
事件处理函数,用来监听页面的上拉触底行为:
// 触底的事件
onReachBottom() {
// 让页码值自增 +1
this.queryObj.pagenum += 1
// 重新获取列表数据
this.getGoodsList()
}
- 改造
methods
中的getGoodsList
函数,当列表数据请求成功之后,进行新旧数据的拼接处理:
// 获取商品列表数据的方法
async getGoodsList() {
// 发起请求
const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
if (res.meta.status !== 200) return uni.$showMsg()
// 为数据赋值:通过展开运算符的形式,进行新旧数据的拼接
this.goodsList = [...this.goodsList, ...res.message.goods]
this.total = res.message.total
}
优化:
通过节流阀防止发起额外的请求
- 在 data 中定义
isloading
节流阀如下:
data() {
return {
// 是否正在请求数据
isloading: false
}
}
- 修改
getGoodsList
方法,在请求数据前后,分别打开和关闭节流阀:
// 获取商品列表数据的方法
async getGoodsList() {
// ** 打开节流阀
this.isloading = true
// 发起请求
const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
// ** 关闭节流阀
this.isloading = false
// 省略其它代码...
}
- 在
onReachBottom
触底事件处理函数中,根据节流阀的状态,来决定是否发起请求:
// 触底的事件
onReachBottom() {
// 判断是否正在请求其它数据,如果是,则不发起额外的请求
if (this.isloading) return
this.queryObj.pagenum += 1
this.getGoodsList()
}
判断数据是否加载完毕
- 如果下面的公式成立,则证明没有下一页数据了:
当前的页码值 * 每页显示多少条数据 >= 总数条数
pagenum * pagesize >= total
- 修改
onReachBottom
事件处理函数如下:
// 触底的事件
onReachBottom() {
// 判断是否还有下一页数据
if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕!')
// 判断是否正在请求其它数据,如果是,则不发起额外的请求
if (this.isloading) return
this.queryObj.pagenum += 1
this.getGoodsList()
}
下一篇:uni-app 数据下拉刷新功能https://blog.csdn.net/SmartJunTao/article/details/123684580
边栏推荐
- MySql main performance indicators description
- 消息队列初见:一起聊聊引入系统mq 之后的问题
- 类型和id对应的两个数组
- 今日份bug,点击win10任务栏视窗动态壁纸消失的bug,暂未发现解决方法。
- 365天挑战LeetCode1000题——Day 053 求解方程 解析 模拟
- 第14章_MySQL事务日志
- PG中的Index-Only Scans解密
- FPGA工程师面试试题集锦71~80
- CAS:2055042-70-9_N-(叠氮基-PEG4)-生物素
- When selecting a data destination when creating an offline synchronization node - an error is reported in the table, the database type is adb pg, what should I do?
猜你喜欢
随机推荐
FPGA:从0开始(安装开发环境)加破解
NPDP|传统行业产品经理如何进行能力提升?
[Teach you how to do mini-games] How to lay out the hands of Dou Dizhu?See what the UP master of the 250,000 fan game area has to say
端口探测详解
关于npm/cnpm/npx/pnpm与yarn
网站架构探测&chrome插件用于信息收集
从企业的视角来看,数据中台到底意味着什么?
杭电多校七 1003-Counting Stickmen(组合数学)
搭建自己的以图搜图系统 (一):10 行代码搞定以图搜图
#yyds干货盘点# 面试必刷TOP101:二分查找-I
C#/VB.NET 将PDF转为PDF/X-1a:2001
Introduction to 3 d games beginners essential 】 【 modeling knowledge
LeetCode·283.移除零·双指针
今日份bug,点击win10任务栏视窗动态壁纸消失的bug,暂未发现解决方法。
Unity_Stack<T>()的应用(多个次级界面后的返回逻辑)
MySQL安装步骤
罗克韦尔Rockwell Automation EDI 项目
「POJ 3666」Making the Grade 题解(两种做法)
西安凯新(CAS:2408831-65-0)Biotin-PEG4-Acrylamide 特性
JVM基本结构