栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

小程序云开发 —— 获取数据库集合里的所有数据

小程序云开发 —— 获取数据库集合里的所有数据

直接在小程序里请求db数据每次最多只能返回20条数据,云函数里请求数据每次最多只能返回100条数据

这个方案是用户上拉加载更多来最终获取所有的数据

const db = wx.cloud.database();
const _ = db.command;
let curPage = 0 // 当前数据第几页
Page({
      data: {
      		isNoMore: false, // 有没更多
      		pubList: []
      },
      onLoad() {
            this.getList()
      },
      getList() {
      		const that = this

			// 此处可以加上加载动画
      		db.collection('publish').where({
                  status: 0
            }).orderBy('creat', 'desc').skip(curPage * 20).limit(20).get({
                  success: function(res) {
                        if (res.data.length < 20) {
                              that.setData({
                                    isNoMore: true
                              })
                        }
                        that.setData({
                              pubList: that.data.pubList.concat(res.data)
                        })
                  },
                  fail() {
                        wx.showToast({
                              title: '获取失败',
                              icon: 'none'
                        })
                  }
            })
      },
      onReachBottom() { // 上拉加载更多
     		if (!this.data.isNoMore) {
     			curPage += 1
            	this.getList()
      		}
      },
      onPullDownRefresh() { // 下拉刷新
      		curPage = 0
      		this.setData({ isNoMore: false }, 
      			() => {
      				this.getList()
			})
      }
})
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/281468.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号