小程序上拉加载的坑

小程序上拉加载的时候,数据请求下来之后会发现数据不会自动叠加,而是一个页面只展示你所请求的数据,所以你要在此把每次请求的数据用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
            })
          }
        }
    })
  },
  

 

posted @ 2018-07-14 15:03  尼古拉斯-富贵  阅读(5498)  评论(0编辑  收藏  举报