分页系列之四:微信小程序滚动加载分页数据

在app.js中添加一个函数,其他页面加载分页数据时,统一调用这个函数。

App({
    loadData: function (that, url, data) {
        that.setData({
            loadText: '正在加载',
            loadClass: 'loadTip icon i-loading' + (that.data.array.length == 0 ? ' big' : '')    //显示一个loading动画
        });
        data.page = that.data.page;    //当前页码
        data.pageSize = that.data.pageSize;    //每页记录数
        data.UserID = wx.getStorageSync('User').ID;    //当前用户ID,如果不需要,可删除
        wx.request({
            url: url,
            data: data,
            success: function (res) {
                //不同于jQuery的AJAX,这里success回调还需要判断状态码,200才表示成功
                if (res.statusCode == 200) {
                    that.setData({
                        array: that.data.array.concat(res.data.List),
                        total: res.data.Total
                    });
                    if (res.data.Total == 0) {
                        that.setData({
                            loadText: '没有相关信息',
                            loadClass: 'loadTip big icon i-info'
                        });
                    }
                    else if (that.data.page == Math.ceil(res.data.Total / that.data.pageSize)) {
                        that.setData({
                            loadText: '全部加载完成',
                            loadClass: 'loadTip'
                        });
                    }
                    else {
                        that.setData({
                            page: that.data.page + 1,
                            loadText: '上滑加载更多',
                            loadClass: 'loadTip'
                        });
                    }
                }
                else {
                    that.setData({
                        loadText: '加载失败,点此重试',
                        loadClass: 'loadTip' + (that.data.array.length == 0 ? ' big icon i-sad' : '')
                    });
                }
            },
            fail: function () {
                that.setData({
                    loadText: '加载失败,点此重试',
                    loadClass: 'loadTip' + (that.data.array.length == 0 ? ' big icon i-sad' : '')
                });
            }
        });
    }
});

上面的loadClass表示调用的CSS类名,相关CSS代码在app.wxss中,必要代码如下,其中使用了图标字体(https://www.iconfont.cn

page {
    background: #eee;
    color: #151516;
    font-size: 32rpx;
    font-family: Arial, Helvetica, sans-serif;
}
.loadTip {
    font-size: 32rpx;
    clear: both;
    width: 100%;
    text-align: center;
    padding-bottom: 16rpx;
}
.loadTip.big {
    margin-top: 48%;
}
.loadTip.big:before {
    font-size: 80rpx;
    color: #a6a6a6;
    width: 100%;
    display: inline-block;
    margin-bottom: 10rpx;
}
.i-loading:before {
    display: inline-block;
    animation: rotate 2s infinite linear;
}
@keyframes rotate {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(359deg);
    }
}
@font-face {font-family: "icon";
    src: url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAABrAAAsAAAAAMnwAABpvAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCIZgrPcL5GATYCJAOBHAtQAAQgBYQ9B4IhG7coVUaFjQMgGfatFUWZXtTZ/x8SuDFEq4G9V6qTMlcO1YV7UepUlAz6WB6UNC5s/lp7Cx1i+bH86EMFI4/r5XZP7pA7OCb6O2M3PjK9l80hQykQnnD57SWbbZPs/tLq3VGbkCgUqgqHESgQDolQOE5iVFdDF5rh+W3+H7iXuBfQS5mIgIAKBuHUuUIpAx0q4toEjIWxGQvFJa4aVw0vcHPbm7q3NP7m2lXq6r/n1FdlAPD8v7e7vX9CD7IMAwooijMpsDzwBAbfzw1LNiXBEt1WDTEMLPffBYvI9rXNAc/XT+a0fykpzEh20szIdrpZoLSPPhCf3nzDAnIu1lW6UqDlYLPbBSwlj8F2/Jq5l0uGpqJojKXTa+8PPbc8wiWYZU0M4GBy///P9WZ/iuTqMO6VheqpMj016r2XoZvMh8x8epNPlCnlZeaXGFyPpyRtTyclIAWAi0KtUCuEWrnC+RVmdS6JlviXAs65LXf/+3N+b3AtDsRoATWsUchYaN/X66FhgBg7MqvOYAJkzkU20NtWaisA5ESYy2hsZCkJZZd1y7xBkPVXvQN4Td68+VG0DKw4XBI+fKEtBioFQ6+gRrM6Q1NzDFjtCiJwBA72pBabA3SHI1GExsPtMQA88P8uE6A4Ka9QxRCAR0gimkKcFDPNlq+IRYky81SpsdHyJn9j6O7Qw6FXY2MoqFw9vHpyzcKuVlpkw5iDrhsPSKquqa6rmdxlKgYy/oM8QJAqWEtzQ7GehS5nJZERXm5gbGKkrWGtr6klUuowZqYqqmrqMBOXygFgPtR9DQUBQoDiRFIoQgGlIFgoDWEJxRDmUAbCEBogKXC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eTlRWBHFUqDg5Cc/7egrYGzHxKz87wn/kqm/P9Q2YtI9ygh3UHjOPuz5wLIWJccg7/8MH8+IEuWRx+kb2pfGfcC/B35gnnb/7EkhfEtIMv2oeHSxyBIA225oYCpWyD+VPDXn/y6r+NJbhWmGFzCsxKtVE+MOFPB+VjRI+ZtAZJd8mUQxcH13XsNT4CYyoG9l1+ikXkrWqV3YiTzXnDVz8VY20cxkfkkikcM7KO3bjflKDXDCKzuvzpSWpxCsruZH/DPyfCaBhX/4BJRpM+HU7znHQu4ikt5+YuI047TrG+lDw/TlHTmNIDkEETy9Xh0tIt0oDSvdmqGEVjdf3WktDicL7un7T/gn5Nhio+wfwCUNYU+9flwyhDuGDRzuF3x5eUvqLE45vjQY9a3zhkmH0PTmb6lASSHUEHP16M3PldUOXSbPOvHSoBcHh7MhpVkRdV0w7Rsx/V8NXUNTS1tHV09fQNDI2MTUzNzC0sraxvbYIhv7k8zJZ/aKfogXYFhCnXfXJ0jGh/fRSv8pLFzcRLw5trXvghrhhMroXmsQJ2tywuZpbaJ6s9UGnyiNM8CbmCjqKic8s2cGC1NicYmPAiFzmKCYE0BNO5LIWoTVcpiPSVj4+LrshyUUVaR6lcAAAA=') format('woff2');
}
.icon {
    font-family: "icon" !important;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
.i-info:before {
    content: "\e727";
}
.i-loading:before {
    content: "\e6ae";
}
.i-sad:before {
    content: "\e610";
}

下面为某个滚动加载数据的页面

pages\purchase\list.wxml

<view id='topBar'>
    <text bindtap="changeMyStatus" class='{{MyStatus == 0 ? "selected" : ""}}' data-mystatus="0">待我审批</text>
    <text bindtap="changeMyStatus" class='{{MyStatus == 1 ? "selected" : ""}}' data-mystatus="1">我已审批</text>
    <text bindtap="changeMyStatus" class='{{MyStatus == 2 ? "selected" : ""}}' data-mystatus="2">已被退回</text>
    <text bindtap="changeMyStatus" class='{{MyStatus == 9 ? "selected" : ""}}' data-mystatus="9">全部信息</text>
</view>
<view wx:for="{{array}}" wx:key="ID" data-id="{{item.ID}}" bindtap="itemTap" class='list'>
    ......
</view>
<view class='{{loadClass}}' bindtap='retry'>{{loadText}}</view>

pages\purchase\list.js

var app = getApp();
Page({
    data: {
        page: 1,
        pageSize: 50,
        array: [],
        MyStatus: 0
    },
    onLoad: function (e) {
        this.loadData();
    },
    onPullDownRefresh: function () {
        this.setData({
            page: 1,
            array: []
        });
        wx.stopPullDownRefresh();
        this.loadData();
    },
    onReachBottom: function () {
        if (this.data.loadText == '上滑加载更多') {
            this.loadData();
        }
    },
    changeMyStatus: function (e) {
        this.setData({
            page: 1,
            array: [],
            MyStatus: e.target.dataset.mystatus
        });
        this.loadData();
    },
    retry: function (e) {
        if (this.data.loadText == '加载失败,点此重试') {
            this.loadData();
        }
    },
    itemTap: function (e) {
        wx.navigateTo({
            url: 'detail?id=' + e.currentTarget.dataset.id
        });
    },
    loadData: function () {
        app.loadData(this, 'https://www.xxx.cn/Purchase/List', {
            MyStatus: this.data.MyStatus
        });
    }
});

pages\purchase\list.wcss

page {
    padding-top: 96rpx;
}
#topBar {
    position: fixed;
    display: flex;
    top: 0;
    width: 100%;
    background: #fff;
}
#topBar text {
    flex: 1;
    text-align: center;
    border-bottom: #d2d2d2 1rpx solid;
    height: 80rpx;
    line-height: 80rpx;
    color: #58595b;
}
#topBar text.selected {
    color: #e02e24;
    border-bottom-color: #e02e24;
}
.list {
    background: #fff;
    margin-bottom: 16rpx;
}
.list .a {
    padding: 22rpx;
    border-bottom: #ededed 1rpx solid;
}
.list .a text {
    float: right;
    color: #e13127;
}
.list .b {
    padding: 10rpx 22rpx;
}
.list .c {
    border-top: #ededed 1rpx solid;
    padding-left: 22rpx;
    height: 86rpx;
    line-height: 86rpx;
}
.list .c text {
    width: 200rpx;
    text-align: center;
    float: right;
    border-left: #ededed 1rpx solid;
}
.list .c text.highlight {
    color: #e13127;
}
text.half {
    display: inline-block;
    width: 50%;
}

 wx.request需要返回如下格式的JSON数据,这里模拟最后一页的数据,每页50条,总记录103条,返回最后3条。

{
    "Total": 103,
    "List": [{
        "ID": 666,
        "Title": "aaa"
    }, {
        "ID": 888,
        "Title": "bbb"
    }, {
        "ID": 999,
        "Title": "ccc"
    }]
}

----分页系列----

一、SQL Server 分页存储过程

二、ASP.NET MVC 网页分页

三、网页滚动加载分页数据

四、微信小程序滚动加载分页数据

五、WinForm + DevExpress 自制分页控件

posted on 2021-04-26 20:16  羊茂林  阅读(1462)  评论(0编辑  收藏  举报

导航