记录一个element-ui级联单选功能

产品提了个需求,级联第一级能选多个,第一级的子级每个只能选一个,也就是一级多选,二级单选。看了下element的文档也没用相关设置,只能搞下change方法了。

首先,用getCheckedNodes(true)获取所有选中的node,遍历node找父级,再找到最后一个点击的node,记录下来,就搞定了。

但发现行不通,返回值不是根据点击顺序返回的,而是根据列表顺序,头大啊。

再来!

更换方法,先判断是否是新的值,新的存起来,旧的替换掉。完成后保存起来,用来下次判断。

changeConfig2(currentValues, form) {
      this.$nextTick(() => {
        // 选中项是否是新的值
        const newValue = currentValues.find(
          val => !this.preSelectedValues.includes(val)
        )
        console.log('newValue', newValue)
        let ref = this.$refs.ruleForm1.$refs.cascaderRef2
        if (newValue) {
          let nodes = ref.getCheckedNodes(true)
          let node = nodes.find(it => it.value === newValue)
          // console.log('node', node, ref)
          if (node) {
            let pid = node.parent.value
            this.selectValue[pid] = node.value
            let ids = Object.values(this.selectValue)
            this.preSelectedValues = ids
          }
        } else {
          this.preSelectedValues = currentValues
        }
      })
    },

方法很好,成功了一半!

又冒出个问题,选中项主动更新后会刷新面板,二级数据的面板被更新成第一条数据的二级面板了,还得改。

changeConfig2(currentValues, form) {
      this.$nextTick(() => {
        const newValue = currentValues.find(
          val => !this.preSelectedValues.includes(val)
        )
        console.log('newValue', newValue)
        let ref = this.$refs.ruleForm1.$refs.cascaderRef2
        if (newValue) {
          let nodes = ref.getCheckedNodes(true)
          let node = nodes.find(it => it.value === newValue)
          // console.log('node', node, ref)
          if (node) {
            let pid = node.parent.value
            this.selectValue[pid] = node.value
            let ids = Object.values(this.selectValue)
            this.preSelectedValues = ids
            this.$nextTick(() => {
              // 设置当前面板的路径,这里居然是个node数组
              ref.$refs.panel.activePath = [node.parent]
              // 同步一下
              ref.$refs.panel.syncActivePath?.()
            })
          }
        } else {
          this.preSelectedValues = currentValues
        }
      })
    },

终于完成了,就是感到比较奇怪,勾选的时候需要重新设置,取消勾选却不需要......

最后还有个问题,关闭弹框就报错,但不影响功能,哎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值