微信小程式雲開發增刪改查
阿新 • • 發佈:2018-12-31
Page({ data: { id:'',//修改用來儲存_id iSshow:true, inpVal:'', inp2Val:'', list:[] }, onLoad: function() { var that = this that.getUserMsg()//讀取資訊 }, //獲取資訊 getUserMsg() { var that = this db.collection('data').get({ success: function(res) { console.log(res) that.setData({ list: res.data }) } }) }, //新增資訊 setUserMsg() { var that = this if (app.globalData.userInfo == undefined) { that.GetUserInfo() return } else { db.collection('data').add({ data: { created: new Date().getTime(), name: that.data.inpVal, age: that.data.inp2Val }, success: function(res) { console.log(res) that.setData({ inpVal: "", inp2Val: "" }) console.log(that.data.inpVal + '--' + that.data.inp2Val) that.getUserMsg() } }) } console.log(app.globalData) }, //刪除資訊 delUserMsg(e) { var that = this var id = e.currentTarget.dataset.id db.collection('data').doc(id).remove({ success: function (res) { console.log(res) that.getUserMsg() } }) }, //修改回顯 changeMsg(e){ var that = this var id = e.currentTarget.dataset.id db.collection('data').doc(id).get({ success: function (res) { that.setData({ inpVal: res.data.name, inp2Val: res.data.age, show: false, id:res.data._id }) } }) }, //更新提交 updetMsg(e){ var that = this var id = e.currentTarget.dataset.id db.collection('data').doc(id).update({ data: { name: that.data.inpVal, age: that.data.inp2Val }, success: function (res) { that.getUserMsg() that.setData({ inpVal: '', inp2Val: '', show: true }) } }) }, })