element ui两棵树互相关联公共部分

14 篇文章 0 订阅

element ui中两棵树关联公共部分,点击一棵树的公共部分的数据,另一棵树也会被选上,取消后 另一棵树也会被取消,如下图所示
请添加图片描述
实现代码:
html部分:

<div style="height:200px;overflow-y: auto;">
        <el-tree v-if="dialogFormVisible"
                 ref="tree"
                 :data="allTsc"
                 :default-checked-keys="TscID"
                 show-checkbox
                 node-key="id"
                 @check-change="handleCheckChange"
                 :props="defaultProps">
        </el-tree>
        <el-tree v-if="dialogFormVisible"
                 ref="lockTree"
                 :data="allLock"
                 :default-checked-keys="LockID"
                 show-checkbox
                 node-key="id"
                 @check-change="handleCheckChange2"
                 :props="defaultProps2">
        </el-tree>
      </div>

js部分:

  data () {
    return {
      all: [],
      allTsc: [],
      allLock: [],
      tsc: [],
      lock: [],
      TscID: [],
      LockID: [],
      defaultProps: {
        children: 'children_node',
        label: 'label'
      },
      defaultProps2: {
        children: 'children_node',
        label: 'label'
      },
    }
  },
 created () {
   this.initLimitationAllTree()
 },
 methods: {
    initLimitationAllTree () { 
      this.loading = true
      getLimitationAllTree().then(response => { // getLimitationAllTree 一个接口获取数据 
        this.limitationsOptions = response.data.data
        let all = []
        let tsc = []
        let lock = []
        this.limitationsOptions.forEach(item => { // 将接口数据分派到两棵树中
          console.log(item)
          if (item.action_scope === "$all$") { // 两棵树的公共数据放在all中
            all.push(item)
          }if (item.action_scope === "$tsc$") {
            tsc.push(item)
          } else if (item.action_scope === "$interlock$") {
            lock.push(item)
          }
        })
        this.all = all;
        this.tsc = tsc;
        this.lock = lock;
        this.allTsc = this.all.concat(tsc); //tsc 的数据
        this.allLock = this.all.concat(lock); //lock的数据
        this.loading = false
      }).catch(error => {
        this.$messageErr.error(error)
        this.loading = false
      })
    },
    handleCheckChange (value, checked) { // tsc
      if(checked) {
        if (value.action_scope ==="$all$") { // 将checked 为true的所有公共部分的数据赋给this.LockID
          this.LockID = this.$refs.tree.getCheckedKeys() // getCheckedKeys 所有数据
        }
      } else {
        if (value.action_scope === "$all$") {
          this.LockID = this.$refs.tree.getCheckedKeys()
          this.allLock = this.all.concat(this.lock); // 刷新Lock下拉Check状态  重新渲染lock的数据
        }
      }
    },
    handleCheckChange2 (value, checked) { // lock
      if(checked) {
        if (value.action_scope === "$all$") {
          this.TscID = this.$refs.lockTree.getCheckedKeys()
        }
      } else {
        if (value.action_scope === "$all$") {
          this.TscID = this.$refs.lockTree.getCheckedKeys()
          this.allTsc = this.all.concat(this.tsc);
        }
      }
    },
 }
<style scoped>
/deep/ .el-tree {
  color: #000000 !important;
  width: 50%;
  float: left;
}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值