【bug】el-input-number null值数据回显0,vxe-table中使用,踩坑记

476 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路 bug如下: 在vex-table可编辑列表中使用了el-input-number ,默认值为空,保存后发现输入框确实没值,但是一点击就会出现一个0。 后端返我空值,null值都不行,因为null 和空值会被el-input-number 组件处理为0,需要定义为undefined 也是昨天沟通了才知道,后端(java)是没有undefined这个概念的 所以他返我null,我要自己处理成undefined

 this.standTableList.forEach(item => {
            if (item.capitaRation == null) item.capitaRation = undefined            
            if (item.frequency == null) item.frequency = undefined
            if (item.configAmount == null) item.configAmount = undefined
          })

还有一种方法,因为vxe-table可编辑列表,其实有两个插槽,默认插槽default和编辑插槽edit 编辑插槽里放el-input-number,默认插槽里放div,然后给div设置禁用属性 这里就要说到我踩的一个坑了,设置属性是放在cell-style方法中,根据条件找到对应列,增加属性。 我遇到的这个坑是我的表头是单表头和双表头结合,有单有双。正常列索引就是从0开始依次往后加一,但是vxe-table不是的,它遇到双表头会重新从0开始,就像下面这样 在这里插入图片描述 所以找对应列的时候要细心点,不然就会像我,找这个bug花了一个多小时

 :cell-style="vxecellStyle"
// vex-table单元格样式
    vxecellStyle({ row, column, columnIndex }) {
      let rowstyle = {}       
        if (columnIndex === 0 || columnIndex === 1 || columnIndex === 3 || columnIndex === 4 || columnIndex === 5) {
          rowstyle.pointerEvents = 'none'
        }   
      return rowstyle
    },

这样直接禁止点击了,也不会出现点击有0的bug了