微信小程序云开发实现数据添加、查询和分页

网友投稿 334 2023-11-08

本文实例为大家分享了微信小程序云开发实现数据添加、查询和分页,供大家参考,具体内容如下

微信小程序云开发实现数据添加、查询和分页

实现的效果

实现要点

WXML 不同类别数据的显示

通过 if-elif-else 实现,在wxml文件中通过<block></block>

渲染,因为它仅仅是一个包装元素,不会在页面中做任何渲染,只接受控制属性。也就是说可以通过属性来控制页面是否要渲染这部分的内容,可以减少页面渲染时间。

云开发数据的获取

先开通云开发功能 ,参考官方文档,然后在创建项目的时候勾选上 使用云开发模板(看个人吧,我直接使用后点击项目中的login)就可以获取到用户的oppenid,之后就可以使用云数据库了。

云开发登录:

云数据的获取

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
console.log(onload);
this.getData(this.data.page);
},
/**
* 获取列表数据
*
*/
getData: function(page) {
var that = this;
console.log("page--->" + page);
const db = wx.cloud.database();
// 获取总数
db.collection(topic).count({
success: function(res) {
that.data.totalCount = res.total;
}
})
// 获取前十条
try {
db.collection(topic)
.where({
_openid: oSly***********vU1KwZE, // 填入当前用户 openid
})
.limit(that.data.pageSize) // 限制返回数量为 10 条
.orderBy(date, desc)
.get({
success: function(res) {
// res.data 是包含以上定义的两条记录的数组
// console.log(res.data)
that.data-ics = res.data;
that.setData({
topics: that.data-ics,
})
wx.hideNavigationBarLoading();//隐藏加载
wx.stopPullDownRefresh();
},
fail: function(event) {
wx.hideNavigationBarLoading();//隐藏加载
wx.stopPullDownRefresh();
}
})
} catch (e) {
wx.hideNavigationBarLoading();//隐藏加载
wx.stopPullDownRefresh();
console.error(e);
}
},

云数据的添加

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 保存到发布集合中
*/
saveDataToServer: function(event) {
var that = this;
const db = wx.cloud.database();
const topic = db.collection(topic)
db.collection(topic).add({
// data 字段表示需新增的 JSON 数据
data: {
content: that.data.content,
date: new Date(),
images: that.data.images,
user: that.data.user,
isLike: that.data.isLike,
},
success: function(res) {
// res 是一个对象,其中有 _id 字段标记刚创建的记录的 id
// 清空,然后重定向到首页
console.log("success---->" + res)
// 保存到发布历史
that.saveToHistoryServer();
// 清空数据
that.data.content = "";
that.data.images = [];
that.setData({
textContent: ,
images: [],
})
that.showTipAndSwitchTab();
},
complete: function(res) {
console.log("complete---->" + res)
}
})
},

云数据的删除

可查看官放文档,这里不贴代码了,有问题联系。

云数据的更新

可查看官放文档,这里不贴代码了,有问题联系。

数据列表的分页

主要就是定义一个临时数组存放加载上来的数据,然后通过传递给对象,最后传递到布局中去。 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持-

您可能感兴趣的文章:Python一键生成核酸检测日历的操作代码利用Python制作简易的核酸检测日历微信小程序 开发之快递查询功能的实现使用微信小程序制作核酸检测点查询工具

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:木鱼小铺新零售小程序怎么样
下一篇:小程序与饿了么、美团的区别?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~