小程序实现上拉加载更多

思路:上拉加载更多(页面上拉触底事件),就是将新获取的数据追加到data的原始数据中

const app = getApp();
Page({
    data: {
        movieList: [],
        page: 1//分页标识,第几次下拉
    },
    onLoad() {
        var that = this;
        this.getMore(that.data.page);
    },
    onReachBottom: function() {
        console.log(1);
        swan.showLoading({
            title: '加载更多',
            make: true
        })
        var that = this;
        this. getMore(that.data.page);
    
    },
    getMore: function (page) {
        var that = this;
        if (page == 1) {
            swan.showLoading({
                title: ' 加载中',
                make: true
            })
        }
        swan.request({
            url: 'https://mip.yesky.com/fishVideo/video/movieList.json',
            method: 'GET',
            data: {
                pageNo: page
            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var res = res.data.movieList;
                if (that.data.page > 1) {
                    var movieLists = that.data.movieList;
                    console.log(movieLists);
                    that.setData({
                        movieList: movieLists.concat(res),
                        page: page + 1
                    })
                } else {
                    that.setData({
                        movieList: res,
                        page: page + 1
                    })
                }
            }, fail: function () {
                swan.showToast({
                    title: '服务器异常',
                    duration: 1500
                })
            },
            complete: function () {
                if (page >= 1) {
                    swan.hideLoading()
                }
            }
        })
    }

})

 

你可能感兴趣的:(小程序)