重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
本文實例為大家分享了微信小程序云開發實現增刪改查的具體代碼,供大家參考,具體內容如下
創新互聯公司堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網站建設、成都網站設計、企業官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯網時代的融安網站設計、移動媒體設計的需求,幫助企業找到有效的互聯網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!
首先按照微信小程序官方提示創建一個快速云開發小程序
大家可以點擊此處下載源代碼
實現效果如下:
在miniprogram->index的下修改下面三個文件
index.js如下:
Page({ data: { id: '',//修改用來保存_id iSshow: true, inpVal: '', inp2Val: '', inp3Val: '', list: [] }, onLoad: function () { var that = this that.getUserMsg()//讀取信息 }, //獲取文本框內容 getName(e) { this.setData({ inpVal: e.detail.value }) }, getAge(e) { this.setData({ inp2Val: e.detail.value }) }, getCreated(e) { this.setData({ inp3Val: e.detail.value }) }, //獲取信息 getUserMsg() { var that = this const db = wx.cloud.database() db.collection('datalist').get({ success: function (res) { console.log(res) that.setData({ list: res.data }) } }) }, //添加信息 setUserMsg() { var that = this const db = wx.cloud.database() db.collection('datalist').add({ data: { name: that.data.inpVal, age: that.data.inp2Val, created: that.data.inp3Val }, success: function (res) { console.log(res) that.setData({ inpVal: "", inp2Val: "", inp3Val:"" }) console.log(that.data.inpVal + '--' + that.data.inp2Val + '--' + that.data.inp2Val) that.getUserMsg() } }) }, //刪除信息 delUserMsg(e) { var that = this const db = wx.cloud.database() var id = e.currentTarget.dataset.id db.collection('datalist').doc(id).remove({ success: function (res) { console.log(res) that.getUserMsg() } }) }, //修改回顯 changeMsg(e) { var that = this var id = e.currentTarget.dataset.id const db = wx.cloud.database() db.collection('datalist').doc(id).get({ success: function (res) { that.setData({ inpVal: res.data.name, inp2Val: res.data.age, inp3Val:res.data.created, show: false, id: res.data._id }) } }) }, //更新提交 updetMsg(e) { var that = this var id = e.currentTarget.dataset.id const db = wx.cloud.database() db.collection('datalist').doc(id).update({ data: { name: that.data.inpVal, age: that.data.inp2Val, created:that.data.inp3Val }, success: function (res) { that.getUserMsg() that.setData({ inpVal: '', inp2Val: '', inp3Val:'', show: true }) } }) }, })
index.wxml如下:
index.wxss如下:
page { background: #f6f6f6; display: flex; flex-direction: column; justify-content: flex-start; font-size: 30rpx; } .box{ width: 90%; display: flex; background: grey } button{ width: 90%; height: 40px; line-height: 40px; margin-top: 20rpx; background:#ffffff; } .infoMsg{ width: 90%; margin: auto; margin-top: 20rpx; border: 1rpx solid #e2e2e2; font-size: 28rpx; } .infoMsg view{ display: flex; border-top: 1rpx solid #e2e2e2; } .infoMsg view>label{ flex: 1; height: 80rpx; line-height: 80rpx; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .infoMsg view>label:not(:first-child){ border-left: 1rpx solid #e2e2e2; } .infoMsg view>label text{ margin-right: 10rpx; border: 1rpx solid #e2e2e2; }
數據集合如下圖:
大家可以點擊此處下載源代碼 ,有問題可以評論交流!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創新互聯。