微信小程序上拉加载下拉刷新要点

本文介绍了如何利用云函数和微信小程序的API实现上拉加载更多数据和下拉刷新功能。通过在云函数中设置skip、limit和orderBy来获取数据库中的数据,并在本地更新数据列表。同时,详细阐述了下拉刷新时如何清空数组并重新加载数据,确保界面正确回弹。
摘要由CSDN通过智能技术生成

上拉加载

1.新建云函数。通过skip、limit、orderBy子句获取数据。

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
    return await cloud.database().collection('playlist')
    .skip(event.start)
    .limit(event.count)
    .orderBy('CreateTime','desc')
    .get()
    .then(res=>{
        return res
    })
}

2.本地编写函数。调用云函数,传递参数。查询云数据库起始Row为本地数组列表的长度 this.data.playlist.length;查询结果要累加至本地数组列表 this.data.playlist.concat(res.result.data),

const MAX_LIMIT=15
_getPlayList(){
            wx.showLoading({
                  title: '加载中...',
                })
                wx.cloud.callFunction({
                      name:'onLinePlayList',
                      data:{
                            start:this.data.playlist.length,
                            count:MAX_LIMIT
                      }
                }).then(res=>{
                      console.log(res.result.data)
                      this.setData({
                            playlist:this.data.playlist.concat(res.result.data)
                      })
                      wx.stopPullDownRefresh()
                      wx.hideLoading()
                })
      },

3.onReachBottom事件调用_getPlayList函数。this._getPlayList函数()


下拉刷新

1.JSON文件中添加"enablePullDownRefresh": true

2.onPullDownRefresh事件清除数组列表内容,调用_getPlayList函数    this.setData({playlist:[]})  this._getPlayList()

3._getPlayList函数中添加 wx.stopPullDownRefresh() ,防止下拉后界面没有回弹。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值