- 加数据10条
- 新建page
- 完成页面的渲染
云函数:
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
return await db.collection("demoUser").limit(event.num).get()
}
页面js:
onLoad: function (options) {
wx.cloud.callFunction({
name: "demoGetList",
data: {
num: 5
}
})
.then(res => {
console.log(res.result.data)
this.setData({
dataList: res.result.data
})
})
},
wxml:
<view class = "row" wx:for="{{dataList}}" wx:key="index">
<view class = "title">{{index + 1}}、{{item.title}}</view>
<view>阅读量:{{item.hits}}</view>
</view>
- 触底加载数据
data: {
dataList: []
},
getData(num = 5, page = 0)
{
wx.cloud.callFunction({
name: "demoGetList",
data: {
num: num,
page: page
}
})
.then(res => {
var oldData = this.data.dataList
var newData = oldData.concat(res.result.data) // 每次加载的时候粘贴
this.setData({
dataList: newData
})
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getData();
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
var page = this.data.dataList.length
this.getData(5, page);
},
云函数调用:
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
return await db.collection("demoUser").skip(event.page).limit(event.num).get()
}