微信小程序之下拉触底时加载下一页 下拉加载更多

本文介绍了如何在微信小程序中使用scroll-view组件实现下拉触底时加载更多数据的功能。通过监听scroll-view的bindscrolltolower事件触发加载下一页,并在onLoad后获取第一页数据,后续页数使用concat方法合并到已有数据中。同时,利用isLoadInterface变量避免接口未完成时重复触发加载,并根据接口返回的islast参数判断是否已加载到最后一页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html:

<scroll-view class='dataContainer' scroll-y="{{true}}" bindscrolltolower="nextDataPage">
      <block wx:for="{{tuijlpList}}" wx:key="index">
            <view >数据:{{item.data}}</view>
      </block>
      <view class="morelp">
      	{{ nomore ? "没有更多了" : "正在加载..." }}
      </view>
      <view class='bottomline' wx:if="{{tuijlpList.length == 0}}" >暂时没有数据</view>
</scroll-view>

js:

data: {
	area_id: "",
    nomore: 0,
    tuijlpList: [],
    isLoadInterface: false,
    pagecount: 1,
    page: 1,
  },
  //查询数据列表
  mgetLoupanList: function () {
    let parmas = {
      area_id: this.data.area_id,
      page: this.data.page
    }

    getLoupanList(parmas).then((res) => {
      // console.log(parmas);
      // console.log(res);
      this.setData({
        pagecount: Math.ceil(res.data.count / 10)
      })
      if (this.data.page == 1) {
        this.setData({
          tuijlpList: res.data.data,
          isLoadInterface: false
        })
      } else {
        this.setData({
          tuijlpList: [...this.data.tuijlpList, ...res.data.data],
          isLoadInterface: false
        })
      }
      // console.log(res);
      // console.log(this.data.page);
      // console.log(this.data.pagecount);
    })
  },
  // 加载下一页数据
  nextDataPage: function () {
    if (!this.data.isLoadInterface) {//防止在接口未执行完再次调用接口
      if (this.data.page * 1 + 1 <= this.data.pagecount) {
        this.setData({
          isLoadInterface: true,
          page: this.data.page * 1 + 1
        })
        this.mgetLoupanList();
      } else {
        //如果大于总页数停止请求数据
        this.setData({
          nomore: 1
        })
        console.log("没有更多了");
      }      
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.mgetLoupanList();
  }

思路:以小程序标签scroll-view作为列表容器,容器方法bindscrolltolower来触发下一页加载

页面onload后执行第一页,非第一页的数据用concat方法拼接之前的数据

防止接口未执行完反复触发bindscrolltolower里的方法,用一个变量isLoadInterface来截断

接口的数据中应有islast这类是否最后一页的参数,用来判断是否加载全部数据

 

借鉴文章:
微信小程序之下拉触底时加载下一页:https://www.cnblogs.com/nanyang520/p/11199719.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值