小程序上拉触底&下拉加载

data: {
    pageNo: 1,//当前页
    pageSize: 10,//每页条数
    count:'',//总条数
    orderList: [],
},
onLoad: function () {
    var that = this;
    var data = `${that.data.type}/${that.data.pageSize}/${that.data.pageNo}`;
    that.getOrderList(data);
},
//订单列表
getOrderList(data) {
    var that = this;
    var orderList = that.data.orderList;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: `${host.orderList}/${data}`,
      header: {
        "content-type": "application/json",
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          if (res.data.data.list && res.data.data.count > orderList.length) {
            var arr = res.data.data.list;
            arr.forEach(item => {
              orderList.push(item);
            })
            that.setData({
              orderList: orderList,
              count: res.data.data.count,
            })
          }
        }
        else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
          })
        }
      },
      complete: function () {
        setTimeout(() => {
          wx.hideLoading();
          wx.stopPullDownRefresh();
        }, 500)
      }
    })
},
/**
  * 页面相关事件处理函数--监听用户下拉动作
*/
  onPullDownRefresh: function () {
    var that = this;
    var data = `${that.data.type}/${that.data.pageSize}/${that.data.pageNo}`;
    that.getOrderList(data);
},
/**
   * 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
    var that = this;
    var pageNo =  that.data.pageNo;
    if (that.data.count > that.data.orderList.length){
      that.setData({
        pageNo: pageNo += 1,
        noMore: true,
      })
      var data = `${that.data.type}/${that.data.pageSize}/${pageNo}`;
      that.getOrderList(data);
    }
},

下拉触底需要在当前页面的json文件添加"enablePullDownRefresh": true或者在app.json window里面全局添加。

原文地址:https://www.cnblogs.com/pycmsj/p/12103065.html