微信小程序—云开发(一)数据知识
作者:秋名
撰写时间:2020 年4月10日
介绍:数据库操作符(倒叙排序,自增,筛选数据,比较大小,指定数据)
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/Command.html
.wxml
<!--pages/fangwenlaing/index.wxml-->
<view class="Datas">
<button bindtap="addData" type="primary" plain size="mini">新增数据</button>
<button bindtap="SelectData" type="default" plain size="mini">查询数据</button>
<button bindtap="ltData" type="primary" plain size="mini">比5小的</button>
<button bindtap="andData" type="primary" plain size="mini">两数之间</button>
<button bindtap="countData" type="primary" plain size="mini">{{count?count+"条数据":"获取条数"}}</button>
</view>
<block wx:for="{{products}}" wx:key="item">
<view class="Datas" bindtap="click" data-id="{{item._id}}">
<image src="{{item.image}}"></image>
<view>名称{{item.title}}</view>
<view>颜色{{item.color}}</view>
<view>数组{{item.tags}}</view>
<view>价格{{item.price}}</view>
<view>访客量{{item.view}}</view>
</view>
<button type="primary"bindtap="delect" size="mini" data-id="{{item._id}}">删除</button>
</block>
.json
{
"usingComponents": {},
"enablePullDownRefresh":true,
"onReachBottomDistance":100
}
.js
const app = getApp()
const db = wx.cloud.database(); //初始化数据库
const _ = db.command
const productsCollection = db.collection('imagelist')
Page({
data: {
data: [],
page: 0,
},
onLoad() {
this.list()
},
//封装请求数据
list() {
let that = this;
// wx.cloud.database().collection('imagelist').get({
productsCollection.orderBy('view', 'desc').get({
success(res) {
console.log(res.data)
that.setData({
products: res.data
})
},
fail(err) {
console.log(res)
}
})
},
//add新增按钮
addData(e) {
// wx.cloud.database().collection('imagelist').add({
productsCollection.add({
data: {
title: "您好.(11)",
image: ["https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg"],
tags: ["ta31", "tag42"],
price: 10.12,
color: "red",
view: 8
},
success(res) {
console.log(res)
},
fail(err) {
console.log(err);
},
})
},
//下拉刷新
onPullDownRefresh() {
this.list()//调用加载方法就好
wx.stopPullDownRefresh();//停止刷新
},
// 下拉加载
onReachBottom() {
let page = this.data.page + 20;//当下拉数据,page+20
// wx.cloud.database().collection('imagelist').skip(page).get().then(res=>{
productsCollection.skip(page).get().then(res => {
let new_data = res.data//声明参数接收数据
let old_data = this.data.products//声明参数接收第二次加载
this.setData({
products: old_data.concat(new_data),//显示的数据,让第二次请求的数据concat(加上新的数据)
page: page
}, res => {
console.log(res);
})
})
},
//doc(获取ID).update()通过点击,一直+1 然后存进数据库(等于修改)
click(e) {
console.log(e.currentTarget.dataset.id);
productsCollection.doc(e.currentTarget.dataset.id).update({
data: {
view: _.inc(1),//点击触发后会访问量+1
// tags:_.push(['tag5','cloud'])//往数组添加一个或多个值
//tags:_.shift()//将数组头部元素删除
// tags: _.pop()//将数组尾部元素删除
}
})
},
// where()查询指定数据
SelectData(e) {
productsCollection.where({
color: "green"
}).get().then(res => {
this.setData({
products: res.data
})
})
},
// lt()筛选小于5的数
ltData() {
productsCollection.where({
price: _.lt(5)
}).get().then(res => {
this.setData({
products: res.data
})
})
},
//.and()筛选数据8~12之间
andData() {
productsCollection.where({
view: _.and(_.gt(8), _.lt(12))
}).get().then(res => {
this.setData({
products: res.data
})
})
},
//count()统计条数
countData() {
productsCollection.where({//可以where筛选数据,再统计数量
view:19
}).count().then(res => {
console.log(res);
this.setData({
count: res.total
})
})
},
// 指定id进行删除
delect(e){
//数据库获取ID删除
console.log(e.currentTarget.dataset.id);
productsCollection.doc(e.currentTarget.dataset.id).remove().then(res=>{
console.log("删除成功"+res);
})
//云函数获取数据删除
// wx.cloud.callFunction({
// name:"remolist"
// }).then(res=>{
// console.log(res);
// })
}
})