小程序的 下拉刷新和上拉加载 文档中也有介绍:
言归正传,那么若想实现下拉刷新这个功能,首先需要在json中写入
{
"enablePullDownRefresh":true,
"backgroundColor":"red"
}
在这里楼主把下拉刷新的背景改为了红色,效果如图:
其中前台wxml代码如下:
<view class="page">
<view class="page__bd">
<view class="weui-panel weui-panel_access">
<view class='nav'>
<!-- <navigator>
<view class="weui-panel__hd">学生列表</view>
</navigator> -->
<form >
<view class="search">
<input confirm-type="search" placeholder="搜索学号/姓名" bindinput='input' value="{{condition}}"/>
<button form-type='submit' size="mini" bindtap='search'>搜索</button>
</view>
</form>
</view>
<view class="weui-panel__bd" wx:for="{{student}}">
<navigator url="../login/login?id={{item.id}}" class="weui-media-box weui-media-box_appmsg" hover-class="weui-cell_active">
<view class="weui-media-box__hd weui-media-box__hd_in-appmsg">
<image class="weui-media-box__thumb" src="{{item.path}}" />
</view>
<view class="weui-media-box__bd weui-media-box__bd_in-appmsg">
<view class="weui-media-box__title">{{item.no}}</view>
<view class="weui-media-box__desc"> {{item.name}} {{item.sex}} {{item.age}}</view>
</view>
</navigator>
</view>
</view>
</view>
</view>
js代码如下:
const app = getApp();
var page = 1;
var isfinish = false;
function loadmore(that) {
if (isfinish) return;
wx.showLoading({
title: '加载中',
})
wx.request({
url: xxx,
data: {
page: page
},
success: (res) => {
wx.hideLoading();
if (res.data.length > 0) {
var student = that.data.student;
for (var i = 0; i < res.data.length; i++) {
student.push(res.data[i]);
}
that.setData({ student: student })
page++;
} else {
isfinish = true;
}
wx.stopPullDownRefresh();
// console.log(res.data)
}
})
}
Page({
data: {
student: []
},
//下拉刷新
onPullDownRefresh: function () {
// console.log(111);
page = 1;
isfinish = false;
this.setData({ student: [] });
loadmore(this);
},
//上拉加载
onReachBottom: function () {
// console.log(222);
var that = this;
loadmore(that);
},
onLoad: function () {
loadmore(this);
}
})
上拉加载就是当你设置的是一页显示10 个内容,当再次往下滑的时候就会显示出来第11-20的内容,以此类推,直到最后。