微信小程序实现上拉刷新下拉加载

const util = require('../../../utils/util.js');

let app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    current_page: 1,
    total: 0,
    PullDownRefreshing: false,  //是否正在刷新
    show_no_data: false,        //展示空数据
    show_loading_more: false,   //展示正在加载中
    has_more: false,            //是否有更多
    show_no_more: false,        //展示没有数据了
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取列表信息
    this.getList();
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      "PullDownRefreshing": true
    });
    //初始化数据
    this.initLoadingData();
    this.getList();
  },
  //获取列表数据
  getList() {
    //获取列表数据
    let form_params = this.getSearchParams();
    util.request('xxxxxxxxx', form_params, 'GET').then((res) => {
      let has_more = res.data.current_page >= res.data.last_page ? false : true;
      let show_no_data = (res.data.current_page <= 1 && res.data.data.length <= 0) ? true : false;
      let list = res.data.current_page > 1 ? this.data.list.concat(res.data.data) : res.data.data;

      this.setData({
        "list": list,
        "total": res.data.total,
        "has_more": has_more,
        "show_no_data": show_no_data
      })
      //停止下拉
      if (this.data.PullDownRefreshing) {
        this.setData({
          "PullDownRefreshing": false
        });
        wx.stopPullDownRefresh();
      }
      //加载更多 处理
      if (this.data.show_loading_more) {
        //界面渲染延迟, 避免列表还未渲染完成就再次触发 loadMore 方法
        setTimeout(function () {
          this.setData({
            "show_loading_more": false,
            "show_no_more": has_more ? false : true
          })
        }.bind(this), 500)
      }
    })
  },
  getSearchParams() {
    return {
      page: this.data.current_page
    }
  },
  //初始化请求数据
  initLoadingData() {
    this.setData({
      "current_page": 1,
      "show_loading_more": false,
      "has_more": false,
      "show_no_more": false,
    })
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.has_more || this.data.show_loading_more) {
      return;
    }

    this.setData({
      "current_page": this.data.current_page + 1,
      "show_loading_more": true
    })
    this.getList();
  },
})

针对上述页面请求返回的json如下

{
	"code": 1,
	"msg": "获取成功",
	"data": {
		"total": 1,
		"per_page": 10,
		"current_page": 1,
		"last_page": 1,
		"data": [
			{
				"id": 30,
				"name": "京东便利店",
			}
		]
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值