小程序分页加载,及下拉刷新

小程序实现分页加载和下拉刷新很简单,因为它在Page中有相关的处理函数

onPullDownRefresh -- 监听用户下拉动作,实现下拉刷新

onReachBottom -- 页面上拉触底,实现分页加载

切记:想实现下拉刷新,必须在页面配置文件中(.json)添加 enablePullDownRefresh:true 开启下拉刷新

Page({
    data:{
        p:1,    //分页请求
        totalpage:null,    //总页数
        isloading:true,    //是否显示加载动画
        newsList:[]    //数据
    },

    obtainNews:function(){
        wx.showLoading({
            title:'加载中...'
        })
        var that=this;
        wx.request({
            url:'url'+that.data.p,
            data: {
                token: that.data.token
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success:res=>{
                if(res.data.status==500){    //没有更多数据
                    wx.showToast({
                        title:'没有数据了',
                        icon:'none'
                    })
                    that.setData({
                        isloading:true
                    })
                }else{
                    var newsArr=that.data.newsList;
                    for(var i=0;i<res.data.info.length;i++){
                        newsArr.push(res.data.info[i])
                    }  
                    that.setData({
                        newsList:newsArr,
                        isloading:true,
                        totalpage:res.data.totalpage
                    })
                }
                
                
            }
        })
    },
    
    onPullDownRefresh:function(){
        wx.showNavigationBarLoading();    //在当前页面显示导航条加载动画
        this.onLoad();    //刷新页面
        setTimeout(function(){
            wx.hideNavigationBarLoading();    //在当前页面隐藏导航条加载动画
            wx.stopPullDownRefresh();    //停止下拉动作
        },2000)
    },
    
    onReachBottom:function(){
        var p=this.data.p;
        var totalpage=this.data.totalpage + 1;
        p++;
        if(p>totalpage){
            return;
        }
        this.setData({
          isloading: false,
          p: p
        })
        this.obtainNews();
    }

})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值