微信小程序下拉刷新上拉加載

1.首先配置app.json(全部頁面均可刷新)或當前頁.json : "enablePullDownRefresh": "true";(注意值是字符串,不是布爾類型)json

2.要點:下拉:onPullDownRefresh: function () {};上拉:onReachBottom: function () {}網絡

3.說明:var pageIndex (頁碼);var rowCount (每頁記錄數);接口返回:totalPages(總頁數)app

4.下拉刷新:清空已經寫入的數據,從新獲取pageIndex=1時的數據;this

上拉加載:每次加載,頁數加1,而後調用接口獲取對應頁碼數據;當前頁面數小於或等於總頁數的時候,則能夠繼續加載;反之,加載完成。url

 代碼舉例code

var pageIndex = 1;
var rowCount = 15;
Page({
  data: {
    salesRecordArr: []
  },
  onLoad: function (options) {
     pageIndex = 1
    saleRecordsRequest(this);
  },
  //下拉 
  onPullDownRefresh: function () {
    pageIndex = 1;
    this.setData({
      salesRecordArr: [],
    });
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 1000
    })
    saleRecordsRequest(this);
  },

  //上拉 
  onReachBottom: function () {
    if (pageIndex <= this.data.dataCount) {
      wx.showToast({
        title: 'loading',
        icon: 'loading',
        duration: 1000
      })
      saleRecordsRequest(this);
    } else {
      wx.showToast({
        title: '已加載完',
        icon: 'loading',
        duration: 1000
      })
    }
  }
})

var saleRecordsRequest = function (that) {
  var params = new Object()
  params.shopId = 「」
  params.pageIndex = pageIndex
  params.rowCount = rowCount

// 網絡請求
  requestServer.POST(
    {
      url: ‘……’,
      tradeId: '...',
      params: params,
      success: function (res) {

        if (!check.checkIsNotNull(res.data)) {
        } else {
          
          var salesRecordArr = that.data.salesRecordArr
          for (var i = 0; i < res.data.body.items.length; i++) {
            salesRecordArr.push(res.data.body.items[i]);
          }
          that.setData({
            salesRecordArr: salesRecordArr,
            dataCount: res.data.body.totalPages
          })
          pageIndex++;
        }
        wx.stopPullDownRefresh();
      },
      fail: function () {
        console.log(res)
      },
    })
}
相關文章
相關標籤/搜索