el-table之toggleRowSelection()运用

博客讨论了一个在 Vue 应用中使用表格组件时遇到的错误:'row is required when getRowIdentity'。作者经过一系列排查,发现问题是由于在调用 `getRowIdentity` 函数时传入了空参数。为了解决这个问题,他们在代码中增加了条件判断,确保在调用该函数之前数据不为空,从而避免了错误的发生。修复后的代码示例展示了如何在数据为空时不触发 `getRowIdentity` 函数,确保了表格操作的稳定性。

报错信息

Error: row is required when get row identity

问题:一直以为是el-table的表数据传输有问题,然后做了一堆排查后发现,其实不然

// 单击或者勾选的数据
    selectPage(e) {
      if(e.constructor === Array){
        this.$refs.maintable.$refs.multipleTable.toggleRowSelection(
          e[0],true
        )
        this.selectRows = e[0]
      }else{
        this.selectRows = e
        this.$refs.maintable.$refs.multipleTable.toggleRowSelection(
          e,true
        )
      }
    },

原因分析:

getRowIdentity 此函数参数为空的时候也执行了,所以导致报错


解决方案:

我们只需要在外层加一个判断,在row为空的时候,不触发getRowIdentity函数即可


完整代码:

// 单击或者勾选的数据
    selectPage(e) {
      //必须要判断是否有选择数据否则toggleRowSelection方法会报错
      if(e.constructor === Array && e.length>0){
        this.$refs.maintable.$refs.multipleTable.toggleRowSelection(
          e[0],true
        )
        this.selectRows = e[0]
      }else{
        this.selectRows = e
        this.$refs.maintable.$refs.multipleTable.toggleRowSelection(
          e,true
        )
      }
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值