【Ant Design Vue中的Tree组件实现子节点的互斥选中】

Tree组件实现子节点的互斥选中

项目中使用Ant Design Vue中的Tree,遇到一个需求,那就是某个节点下的所有叶子节点实现互斥勾选,即勾选了A,再去勾选B的话,自动去掉A,反之,亦然。
如图,需要实现洗发指令和开灯X小时的互斥
在这里插入图片描述
代码

<Tree
                checkable
                :checkStrictly = "checkStrictly"
                @expand="onExpand"
                :expandedKeys="expandedKeys"
                :autoExpandParent="autoExpandParent"
                v-model ="checkedKeys"
                @select="onSelect"
                @check="onCheck"
                :selectedKeys="selectedKeys"
                :treeData="treeData"
            />
        onCheck(checkedKeys,a,b) {
            let curCheckId = a.node._props.eventKey
            let halfTemp = []
            if(a.checked){
            //判断是否第一次加载树
                if(this.isFirsrtLag){
                   this.uncheckId = (this.$store.state.buttonsArray.indexOf("2601") == -1) ? "2601" : "2603"        
                }
                let tempArray = checkedKeys
                //判断是否选择的是叶子节点
                if(curCheckId == '2601' || curCheckId == '2603'){
                   
                    let oncurCheckId = ""
                    if(!this.isFirsrtLag){
                        oncurCheckId = curCheckId == '2601' ? '2603' : '2601'
                    }else{
                        oncurCheckId = this.uncheckId == '2601' ? '2603' : '2601'
                    }
                    // }
                    checkedKeys = [].concat(tempArray.filter( item => item != oncurCheckId))
                   
                    this.uncheckId = oncurCheckId
                    this.isFirsrtLag = false
                }else{
                //选择的该叶子节点的父节点或是祖父节点
                    if(curCheckId == '1067246875800000040' || curCheckId == '1227912572663906305'){
                        checkedKeys = [].concat(tempArray.filter( item => item != this.uncheckId))
                    }
                }
                checkedKeys = [].concat(checkedKeys.filter( item => (item != '1067246875800000040') && (item != '1227912572663906305')))
                halfTemp = ["1067246875800000040",'1227912572663906305'];
            }
            this.checkedKeys = checkedKeys;
            this.halfCheckedKeys = halfTemp.concat(a.halfCheckedKeys ? a.halfCheckedKeys : [])
        },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值