13加载下一页数据

  1. 用户上滑页面 滚动条触底,开始加载下一页数据
    1. 找到滚动条触底事件 官方文档寻找。
    2. 判断还有没有下一页数据
      1. 获取到总页数,但现在只有总条数
        总页数 = Math.ceil(总条数total / 页容量 pagesize)
        总页数 = Math.ceil( 23 / 10 ) = 3
    3. 没有下一页数据,弹出提示
    4. 有下一页,加载下一页。
      1. 当前的页码 ++
      2. 重新发送请求
      3. 数据请求回来 要对data中的数组进行拼接,而不是全部替换

goods_list/index.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList:[]
  },
  totalPages:1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options);
      this.QueryParams.cid=options.cia;
      this.getGoodList();
  },
  async getGoodList(){
    const res = await request({url:"/goods/search",data:this.QueryParams});
    console.log(res);
    //获取总条数
    const total = res.total;
    //计算总页数
    this.totalPages = Math.ceil(total/this.QueryParams.pagesize);
    console.log(this.totalPages);
    this.setData({
      //拼接了数组
      goodsList:[...this.data.goodsList,...res.goods]
    })
  },
  //页面上滑   滚动条触底事件
  onReachBottom(){
    // console.log("页面触底");
    if(this.QueryParams.pagenum>=this.totalPages){
      //没有下一页
      // console.log('%c'+"没有下一页数据了","color:red;font-size:100px;background-image");
      wx.showToast({
        title:'没有数据了'
      })
    }else{
      // console.log('%c'+"有下一页数据","color:red;font-size:100px;background-image");
      this.QueryParams.pagenum++;
      this.getGoodList();
    }
  }
})
  • 初始化 totalPages:1
  • 拼接数组,不是覆盖!
  • 页面++
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值