微信小程序云開發(fā)之模擬后臺(tái)增刪改查
小程序云開發(fā)出來之后,小程序開發(fā)人員也要慢慢的接觸后端對(duì)數(shù)據(jù)的增刪改查了。下面就給大家提供一個(gè)案例吧。

這里我把新增和修改放在了一個(gè)頁面

顯示頁面index.wxml
<view wx:if="{{books}}" class='container'>
<view class='title'>
<text>圖書列表</text>
</view>
<view class='label'>
<text>書名</text>
<text>作者</text>
<text>價(jià)格</text>
<text>操作</text>
</view>
<block wx:for="{{books}}" wx:key="">
<view class='content'>
<text>{{item.name}}</text>
<text>{{item.author}}</text>
<text>{{item.price}}</text>
<button class='del' data-id='{{item._id}}' bindtap='onDel'>刪除</button>
<button class='update' data-id='{{item._id}}' bindtap='onUpdate'>修改</button>
</view>
</block>
</view>
<view wx:else="{{books}}" class='none'>
<text >暫時(shí)沒有圖書!</text>
</view>
<view class='add'>
<button bindtap='goSet'>添加圖書</button>
</view>
index.js
// pages/index/index.js
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
books:[]
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
const db = wx.cloud.database()
db.collection("books").get({
success:res=>{
this.setData({
books:res.data
})
},fail:err=>{
wx.showToast({
icon:"none",
title: '查詢記錄失敗',
})
}
})
},
goSet:function(){
wx.navigateTo({
url: '../set/set',
})
}, onDel:function(e){
let id = e.currentTarget.dataset.id
const db = wx.cloud.database();
db.collection("books").doc(id).remove({
success:res=>{
wx.showToast({
title: '刪除成功',
})
this.onLoad()//刪除成功重新加載
},fail:err=>{
wx.showToast({
title: '刪除失敗',
})
}
})
console.log(id)
},onUpdate:function(e){
let id = e.currentTarget.dataset.id
wx.navigateTo({
url: '../set/set?id='+id,
})
}
})
添加和修改共用set.wxml
<!--pages/set/set.wxml-->
<view class='container'>
<form bindsubmit='comfirm' >
<view class='input-container'>
<label>書名:</label>
<input style='display:none' data-value='{{id}}' name="id" value='{{book._id}}'></input>
<input data-value='{{name}}' name="name" value='{{book.name}}'></input>
</view>
<view class='input-container'>
<label>作者:</label>
<input data-value='{{author}}' name="author" value='{{book.author}}'></input>
</view>
<view class='input-container'>
<label>價(jià)格:</label>
<input data-value='{{price}}' name ="price" value='{{book.price}}'></input>
</view>
<view class='comfirm'>
<button form-type='submit'>保存</button>
</view>
</form>
</view>
set.js
// pages/set/set.js
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
book:[]
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
if(options.id){
const db = wx.cloud.database();
db.collection("books").where({
_id:options.id
}).get({
success:res=>{
this.setData({
book:res.data[0]//返回的是一個(gè)數(shù)組,取第一個(gè)
})
},fail:err=>{
console.log(err)
}
})
}
},
comfirm:function(e){
const db = wx.cloud.database()//打開數(shù)據(jù)庫連接
let book = e.detail.value
if(book.id==""){//id等于空是新增數(shù)據(jù)
this.add(db,book) //新增記錄
}else{
this.update(db,book) //修改記錄
}
}, add: function (db, book) {
db.collection("books").add({
data: {
name: book.name,
author: book.author,
price: parseFloat(book.price)
}, success: res => {
wx.showToast({
title: '新增記錄成功',
})
wx.navigateTo({
url: '../index/index',
})
}, fail: err => {
wx.showToast({
title: '新增失敗',
})
}
})
}, update: function (db, book) {
db.collection("books").doc(book.id).update({
data: {
name: book.name,
author: book.author,
price: parseFloat(book.price)
}, success: res => {
wx.showToast({
title: '修改記錄成功',
})
wx.navigateTo({
url: '../index/index',
})
}, fail: err => {
wx.showToast({
title: '修改失敗',
})
}
})
}
})
云開發(fā)后臺(tái)數(shù)據(jù),需要手動(dòng)添加books集合:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序?qū)崿F(xiàn)獲取準(zhǔn)確的騰訊定位地址功能示例
- 微信小程序?qū)Ш綑诨瑒?dòng)定位功能示例(實(shí)現(xiàn)CSS3的positionsticky效果)
- 微信小程序使用map組件實(shí)現(xiàn)檢索(定位位置)周邊的POI功能示例
- 微信小程序使用map組件實(shí)現(xiàn)獲取定位城市天氣或者指定城市天氣數(shù)據(jù)功能
- 基于騰訊云服務(wù)器部署微信小程序后臺(tái)服務(wù)(Python+Django)
- PHP后臺(tái)實(shí)現(xiàn)微信小程序登錄
- 微信小程序wx.request實(shí)現(xiàn)后臺(tái)數(shù)據(jù)交互功能分析
- 微信小程序后臺(tái)持續(xù)定位功能使用詳解
相關(guān)文章
Extjs gridpanel 中的checkbox(復(fù)選框)根據(jù)某行的條件不能選中的解決方法
這篇文章主要介紹了Extjs gridpanel 中的checkbox(復(fù)選框)根據(jù)某行的條件不能選中的解決方法,需要的朋友可以參考下2017-02-02
用javascript實(shí)現(xiàn)li 列表數(shù)據(jù)隔行變換背景顏色
客戶端效果,效率自然不錯(cuò)。以前的做法是偶數(shù)行時(shí)給li加一個(gè)class,方法當(dāng)然不可取,如果后臺(tái)讀取再加class就很麻煩了,看看這個(gè)效果2007-08-08
JavaScript實(shí)現(xiàn)反轉(zhuǎn)字符串的方法詳解
這篇文章主要介紹了JavaScript實(shí)現(xiàn)反轉(zhuǎn)字符串的方法,結(jié)合實(shí)例形式分析了字符串反轉(zhuǎn)操作,并詳細(xì)講述了相關(guān)函數(shù)的功能與使用注意事項(xiàng),需要的朋友可以參考下2017-04-04
javascript設(shè)置金額樣式轉(zhuǎn)換保留兩位小數(shù)示例代碼
本文為大家介紹下javascript設(shè)置金額樣式即保留兩位小數(shù),下面有個(gè)不錯(cuò)的教程,需要的朋友可以了解下2013-12-12
分頁欄的web標(biāo)準(zhǔn)實(shí)現(xiàn)
分頁欄是網(wǎng)頁上最常見不過的一個(gè)組件,本博文給出分頁欄的一個(gè)web2.0標(biāo)準(zhǔn)示例,并作簡要分析2011-11-11
JavaScript數(shù)組的定義及數(shù)字操作技巧
這篇文章主要介紹了JavaScript數(shù)組的定義及數(shù)字操作技巧的相關(guān)資料,需要的朋友可以參考下2016-06-06
js學(xué)習(xí)總結(jié)_基于數(shù)據(jù)類型檢測(cè)的四種方式(必看)
下面小編就為大家?guī)硪黄猨s學(xué)習(xí)總結(jié)_基于數(shù)據(jù)類型檢測(cè)的四種方式(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07

