elementUI编辑与新增弹窗结合表格常见bug解决方案

3、新增、编辑弹窗出现的问题

编辑弹窗修改数据后,点击取消,表格数据变化;编辑弹窗修改后,不做任何操作,点击下一条数据进行编辑,取消,数据被上一条覆盖,需要刷新页面才能还原;新增和修改弹窗的数据发生紊乱等等问题的解决方法

1、组件弹窗中的props定义

props: {
  ObjectData: {
    require: true,
    type: Object,
    default: () => {}
  },
  // 传入编辑id
  modityId:{
    type: String,
    required: false,
    default: ''
  },
  //弹窗的显示
  visible: {
    type: Boolean,
    required: false,
    default: false
  },
},

2、return data数据定义

// 校验规则

rules: {
  function: [
    {required: true, message: 'Please enter 【Function】', trigger: 'blur'}
  ]
},
// 搜索条件
searchType:{
  function:'',
  subFunction:'',
  area:''
},

3、编写监听事件

watch:{
  visible: {
    handler(n, o) {
      // debugger;
      if (n && this.ObjectData.id) {
        this.modityData(this.modityId)
        this.searchType =  this.ObjectData;
      } else if ((this.$refs['form'] && !this.modityId)) {
        this.$refs['form'].resetFields();
        this.searchType = {
          function: '',
          subFunction: '',
          area: ''
        };
      }
    },
    deep: true,
    immediate: true
  }
},

4、编写方法

// 编辑表单数据有id

modityData(id){

   this.searchType.id = id;

},

 

5、在父组件中调用子组时,需要绑定这个id,当为编辑时候给id赋值this.modityid = row.id;;新增时,id为空

6、当打开编辑弹窗时,赋值代码写:this.updateObject = Object.assign({},row);这么写的目的是为了避免深拷贝造成的麻烦,就是当弹窗上的值变化时候,表格数据也变化,主要解决这个问题。updateObject为绑定在子组件上的数据

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值