溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信小程序中如何實現(xiàn)列表上拉加載

發(fā)布時間:2021-01-28 10:39:10 來源:億速云 閱讀:251 作者:小新 欄目:移動開發(fā)

小編給大家分享一下微信小程序中如何實現(xiàn)列表上拉加載,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

某個頁面,有多個列表,如100行,這時需要實現(xiàn)分頁功能,手機端的分頁一般都是滑到底部時上拉刷新。

使用scroll-view實現(xiàn),其bindscrolltolower方法:滾動到底部/右邊觸發(fā)。當觸發(fā)時發(fā)送請求獲取新的數(shù)據(jù),我寫的時候獲取的數(shù)據(jù)很快,我還給它加了個定時器啊哈哈哈,因為我想讓showLoading加載彈窗轉(zhuǎn)一轉(zhuǎn),讓用戶知道上拉刷新數(shù)據(jù)。因為沒加的時候showLoading一閃而過,感覺體驗效果不好。

最后scroll-view使用豎向滾動時,需要給<scroll-view/>一個固定高度(height:93%),再給page設(shè)置高度(height:100%),否則bindscrolltolower觸發(fā)不了

<scroll-view wx:if="{{isShowList}}" class='scrollHeight' scroll-y="true" bindscrolltolower="getMore" lower-threshold='3'>
</scroll-view>

來一段邏輯的代碼

  //上拉加載分頁
  getMore(e){
    var that = this;
    var user = wx.getStorageSync('bizUser');
    wx.showLoading({
      title: '正在加載中',
    });
    setTimeout(function(){
      var pageindex = that.data.curPage;
      var student = that.data.student;
      if (pageindex>=1){
        ++pageindex;
      }
      wx.request({
        url: app.url + '',
        data: {
          schoolId: user.schoolId,
          pageSize: 10,
          curPage:pageindex
        },
        method: 'GET',
        success:function(res){
          if (res.data.data) {
            var studentLength = (res.data.data instanceof Array) ? res.data.data.length : 0;
            for (var i = 0; i < studentLength; i++) {
              //判斷計時付或一次性
              if (res.data.data[i].sign_type == 2) {
                res.data.data[i].sign_type = '一次性';
              } else if (res.data.data[i].sign_type == 1) {
                res.data.data[i].sign_type = '計時付';
              } else if (res.data.data[i].sign_type == 3) {
                res.data.data[i].sign_type = '計時付';
              } else if (res.data.data[i].sign_type == 4) {
                res.data.data[i].sign_type = '一次性';
              } else if (res.data.data[i].sign_type = 5) {
                res.data.data[i].sign_type = '一次性'
              }

              //數(shù)字變中文
              if (res.data.data[i].learn_stage == 1) {
                res.data.data[i].learn_stage = '一';
              } else if (res.data.data[i].learn_stage == 2) {
                res.data.data[i].learn_stage = '二';
              } else if (res.data.data[i].learn_stage == 3) {
                res.data.data[i].learn_stage = '三'
              }
            }

            if (studentLength ==10) {
              for (var j = 0; j < studentLength;j++){
                student.push(res.data.data[j]);
              }
              that.setData({
                student: student,
                load: '上拉加載更多..',
                curPage: pageindex
              })


            } else if (studentLength<10){
              for (var j = 0; j < studentLength; j++) {
                student.push(res.data.data[j]);
              }
              that.setData({
                student: student,
                load: '已經(jīng)沒有更多了..',
                curPage: pageindex
              })
            }
          } else {
            that.setData({
              load: '已經(jīng)沒有更多了'
            })
          }
        }
      })

      wx.hideLoading();

    },500)

  },

看完了這篇文章,相信你對“微信小程序中如何實現(xiàn)列表上拉加載”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI