小程序隨筆10:列表下拉刷新Toast顯示加載效果

小程序列表下拉上拉加載數(shù)據(jù)的方式很多種。
上一篇是結(jié)合導(dǎo)航欄loading顯示正在加載的效果。請(qǐng)參考地址:http://m.itdecent.cn/p/a1903cb4e1be
本篇利用本地假數(shù)據(jù)結(jié)合wx.showToast來展示。
注意:列表實(shí)現(xiàn)前面文章中已實(shí)現(xiàn),本篇只特別注明修改的地方。請(qǐng)參考地址:http://m.itdecent.cn/p/44b07a86c8ff

實(shí)現(xiàn)方法:
1、在小程序app.json文件中,對(duì)應(yīng)的window下添加代碼:

"enablePullDownRefresh": true,

2、在小程序頁面對(duì)應(yīng)的.js文件中,數(shù)據(jù)初始化部分,newList后面添加totalList用于展示當(dāng)前列表的所有數(shù)據(jù)。

const newList = new Array(10).fill(0)
var totalList = newList;

3、在小程序頁面對(duì)應(yīng)的.js文件中,添加data:

data: {
    list:totalList,
    pageNum: 1,       // 設(shè)置加載的第幾次,默認(rèn)是第一次
    pageTotalNum:2,   // 設(shè)置總共的分頁數(shù)
  },

4、在小程序頁面對(duì)應(yīng)的.js文件Page下添加onPullDownRefresh和onReachBottom方法。

//上拉加載更多
  onReachBottom:function(params) {
    this.data.pageNum += 1;
    if(this.data.pageNum > this.data.pageTotalNum){
        wx.showToast({
          title: '沒有數(shù)據(jù)了',
        })
        return
    }
    wx.showToast({
      title: '加載更多',
      icon:"loading"
    })
    wx.request({
      url: 'https://wwww.test.com',
      complete: res=>{
        var templist = new Array(totalList.length + 10).fill(0)
        for (let index = 0; index < totalList.length; index++) {
          templist[index] = totalList[index];
        }
        for (let i = totalList.length; i < templist.length; i++) {
          templist[i] = {
            idx: i+1,
            nickname: `nickname${i+1}`,
            datetime: `datetime${i+1}`,
            last_content: `內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容`,
            head_img_url: imgUrlList[(i) % 5],
          }
        }
        totalList = templist;
        this.setData({
          list:totalList,
          pageNum:this.data.pageNum
        })
        wx.hideToast();
      }
    })
  },

  //下拉刷新
  onPullDownRefresh:function(options) {
    wx.showToast({
      title: 'loading...',
      icon:"loading"
    })
    wx.request({
      url: 'https://wwww.test.com',
      complete: res=>{
        wx.hideToast();
        wx.stopPullDownRefresh()
        totalList = null;
        totalList = newList;
        this.setData({
          list:totalList,
          pageNum:1
        })
        console.info(totalList.length);
      }
    })
  },

此項(xiàng)目主要是本地假數(shù)據(jù)結(jié)合wx.showToast顯示正在加載的效果,隨筆記錄,不喜勿噴。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容