|
楼主 |
发表于 2025-2-27 12:26:48
|
显示全部楼层
本帖最后由 8哥爱前端 于 2025-2-27 12:28 编辑
数据处理
对每次查询到的数据进行处理。
变量 current 保存从页面 data 中获取已加载到页面的数据(数组);
通过 .concat(). 方法将最新查询到的数据合并到之前的数据之后,形成新的数组,赋给变量 newList
这里通过限制 page 的值来避免用户无限次加载查询数据库(page 限制最高80,加上最初的20,总共能获取100条数据)
通过 this.setData() 将最新数据更新,从而使前端页面通过数据绑定实现加载更多内容的功能。
总结
以上就是不通过云函数,查询小程序“云开发”数据库的案例方法。.js页面的所有代码如下:
- const db = wx.cloud.database()
-
- Page({
- data: {
- showList:[]
- },
- // getData()查询方法
- getData(rows=20,page=0){ //rows:每次查询条数,page:每次查询跳过的条数
- db.collection('name').orderBy('num','desc').skip(page).limit(rows).get().
- then( res => {
- let current = this.data.showList
- let newList = current.concat(res.data)
- this.setData({
- showList:newList
- })
- })
- },
- onLoad: function () {
- this.getData() // 页面加载时,第一次查询数据
- },
- // 用户点击按钮时,再次查询并更新数据
- showMore: function() {
- let page = this.data.showList.length // 获取已查询数据条数
- if (page <= 80) { // 限制查询条数
- this.getData(20,page) // 函数内调用getData()查询方法
- }else{
- wx.showToast({
- title: '不能超过100条结果',
- icon:'error',
- duration:3600,
- })
- }
- }
- })
复制代码
|
|