tree相互控制是否能点击

请添加图片描述

<template>
  <div style="display:flex">
    <div class="gujia_one">
      <el-tree :data="treeData1" default-expand-all ref="tree" node-key="id" :show-checkbox="true" el-tree="id"
        @check-change="handleCheckChange" :expand-on-click-node="false">
      </el-tree>
    </div>
    <div class="gujia">
      <el-tree ref="tree2" :data="treeData2" node-key="id" :default-expand-all="true" :expand-on-click-node="false"
        :show-checkbox="true" @check-change="handleCheckChange2">
      </el-tree>
    </div>
  </div>
</template>
<script>
  export default ({
    data() {
      return {
        treeData1: [{
            id: 1,
            label: '普通公寓',
            level: 'one',
            check: false,
            children: [{
              id: 3,
              label: '3.0基准价法',
              level: 'one',
              disabled: false,
              check: false

            }, {
              id: 2,
              label: '兼容估价法',
              level: '',
              disabled: false,
              check: false
            }]
          },
          {
            id: 4,
            label: '独栋别墅',
            level: 'one',
            check: false,
            children: [{
              id: 5,
              label: '3.0基准价法',
              level: 'one',
              disabled: false,
              check: false

            }, {
              id: 6,
              label: '兼容估价法',
              level: '',
              disabled: false,
              check: false
            }]
          }
        ],
        treeData2: [{
            id: 113,
            disabled: true,
            children: [{
              id: 13,
              label: '4.0股价法',
              disabled: false,
              level: 'one_men',
              check: false
            }, {
              id: 12,
              label: '案例估价法',
              disabled: false,
              level: '',
              check: false
            }]
          },
          {
            id: 1154,
            disabled: true,
            children: [{
              id: 4,
              label: '4.0股价法',
              disabled: false,
              level: 'one_men',
              check: false
            }, {
              id: 22,
              label: '案例估价法',
              disabled: false,
              level: '',
              check: false
            }]
          }
        ],
        inde: null
      }
    },
    methods: {

      handleCheckChange(data, checked) {
        const parentKey = this.$refs.tree.getNode(data).parent.key
        const parentNodeIndex = this.treeData1.findIndex(node => node.id === parentKey)
        this.treeData1.forEach(node => {
          const menu = node.children.find(child => child.id === data.id)
          if (!menu) {
            return
          }
          if (checked && data.level === 'one') {
            node.check = true
            menu.check = true
            this.treeData2[parentNodeIndex].children.forEach(child => {
              if (!child.disabled) {
                child.disabled = true
                const tree = this.$refs.tree2 // 获取 el-tree 实例对象,勾选3.0基准法的时候把,treeData2的树形取消勾选
                tree.setCheckedKeys([])
              }
            })
          } else {
            if (checked == false && data.level == 'one') {
              node.check = false
            }
            if (!node.check) {
              menu.check = false
              this.treeData2[parentNodeIndex].children.forEach(child => {
                if (child.disabled) {
                  child.disabled = false
                }
              })
            }
          }
        })
        console.log(this.$refs.tree.getCheckedNodes());//获取勾选中的对象可以取id值
      },
      handleCheckChange2(data, checked) {
        const parentKey = this.$refs.tree2.getNode(data).parent.key
        const parentIndex = this.treeData2.findIndex(node => node.id === parentKey)
        if (checked && parentIndex !== -1) {
          const targetChild = this.treeData1[parentIndex].children[0]
          if (data.level === 'one_men') {
            const targetMenu = this.treeData2[parentIndex].children.find(menu => menu.id === data.id)
            if (targetMenu) {
              targetMenu.checked = true
              targetChild.disabled = true
            }
          }
        } else if (!checked && parentIndex !== -1) {
          const targetChild = this.treeData1[parentIndex].children[0]
          const targetMenu = this.treeData2[parentIndex].children.find(menu => menu.id === data.id)
          if (targetMenu) {
            targetMenu.checked = false
            targetChild.disabled = false
          }
        }
      },
    }
  })
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值