前端生成一个随机的id,解决前端新增和删除问题

generateId() {
  const s = [];
  const hexDigits = '0123456789abcdef';
  for (let i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  // bits 12-15 of the time_hi_and_version field to 0010
  s[14] = '4';
  // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  s[8] = s[13] = s[18] = s[23] = '-';

  const uuid = s.join('');
  return uuid;
},

主要解决的问题是:

     当不经过数据库操作,新增和删除只是前端进行简单的处理,等点击save才进入数据库时,这时候需要考虑随机数的应用了。

新增的时候,进行push操作 

this.dataList.push({
  name:'',
  titile:'',
  id:this.generateId()
})

删除的时候

/** 删除数据的方法*/
deleteData(row){
  // 你确定要删除这条数据嘛?
  this.$confirm('Are you sure you want to delete this data?', 'Delete', {
    cancelButtonText: 'Cancel',
    confirmButtonText: 'OK',
    type: 'warning'
  }).then(() => {             this.dataList = this.dataList.filter(item => (item.id !== row.id));  }).catch(() => {
  });

},
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值