微信小程序•云开发增删改查

微信小程序•云开发增删改查_第1张图片

html


  
    
    
  
  
    
    
  
  
  





  
    
    
    
    
  
  
    
    
    
    
  


js

Page({
  data: {
    id:'',//修改用来保存_id
    iSshow:true,
    inpVal:'',
    inp2Val:'',
    list:[]
  },
  onLoad: function() {
    var that = this
    that.getUserMsg()//读取信息
  },
//获取文本框内容
  getName(e) {
    this.setData({
      inpVal: e.detail.value
    })
  },

  getAge(e) {
    this.setData({
      inp2Val: e.detail.value
    })
  },
//获取信息
  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
        })
      }
    })
  },
})

css

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;
}

云数据库

微信小程序•云开发增删改查_第2张图片

方法很简单,文档直接拿来用就好了,在不懂的小伙伴,可以关注私信我!

有帮助到大家的关注我

会一直更新小程序常用技术及新玩法。

微信小程序•云开发增删改查_第3张图片

 

喜欢上方小程序,需要源码的,私信小编留下邮箱。

你可能感兴趣的:(小程序,微信小程序开发)