小程序上拉加载的坑


小程序上拉加载的时候,数据请求下来之后会发现数据不会自动叠加,而是一个页面只展示你所请求的数据,所以你要在此把每次请求的数据用concat连接起来,千万不能用push添加,直接上代码

 
 
   data: { //全局变量
      list:'',
      title:'',
      id:'',
      currentPage:'1',
      hasmore:false
     }
 
onReachBottom() {  //上拉触底函数
   let that = this   //注意that
    console.log(that.data.id)
    console.log(that.data.currentPage++)
    console.log(that.data.list)
    wx.request({
      url: 'http://www.zhm365.com/zhm/api/loadNewByTitle',
      data: {
        title:that.data.id,
        pageSize: '10',
        currentPage:that.data.currentPage++  // 请求页面不能定死,每次请求页面自增,如果页面固定死的话,每次请求的数据相同
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data.info)
          if(res.data.info!==null){
            //每次刷新的数据叠加,注意是用concat进行连接,而不是用push添加
            that.setData({
              list: that.data.list.concat(res.data.info),
              hasmore: true
            })
          }else{
            that.setData({
              hasmore: true
            })
          }
        }
    })
  },
  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM