antd vue tree 选中的菜单显示单独取消选中,tree取消子级选中 关联父级同时取消选中


<template>
  <div class="wrapper">
    <a-tree v-model="checkedKeys" checkable defaultExpandAll @check="onCheck" :tree-data="treeData" />
    <div>
      <a-tag v-for="(tag,index) in tags" :key="tag.key" closable v-if="tag.isShow" @close="handleClose(tag.key,index)">
        {{tag.title}}
      </a-tag>
    </div>
  </div>
</template>

<script>
const treeData = [
  {
    title: '0-0',
    key: '0-0',
    children: [
      {
        title: '0-0-0',
        key: '0-0-0',
        children: [
          { title: '0-0-0-0', key: '0-0-0-0' },
          { title: '0-0-0-1', key: '0-0-0-1' },
          { title: '0-0-0-2', key: '0-0-0-2' },
        ],
      },
      {
        title: '0-0-1',
        key: '0-0-1',
        children: [
          { title: '0-0-1-0', key: '0-0-1-0' },
          { title: '0-0-1-1', key: '0-0-1-1' },
          { title: '0-0-1-2', key: '0-0-1-2' },
        ],
      },
      {
        title: '0-0-2',
        key: '0-0-2',
      },
    ],
  },
  {
    title: '0-1',
    key: '0-1',
    children: [
      { title: '0-1-0-0', key: '0-1-0-0' },
      { title: '0-1-0-1', key: '0-1-0-1' },
      { title: '0-1-0-2', key: '0-1-0-2' },
    ],
  },
  {
    title: '0-2',
    key: '0-2',
  },
]

export default {
  components: {},
  data() {
    return {
      checkedKeys: ['0-0-0-1', '0-0-0-2'],// 选中数据
      treeData, 
      tags: [], //菜单
    }
  },
  mounted() {
    // 初始化获取选中数据的title
    this.getTagsData(this.treeData, this.tags)
  },
  methods: {
    // 复选框事件
    onCheck(checkedKeys, e) {
      this.checkedKeys = checkedKeys
      this.tags = []
      e.checkedNodes.map((item) => {
        this.tags.push({
          key: item.key,
          title: item.data.props.title,
          isShow: item.data.props.dataRef.children ? false : true, // 有子级的父级不显示
        })
      })
    },
    // 菜单删除事件
    handleClose(key, index) {
      this.tags.splice(index, 1)
      let result=[]
      this.getParentId(key,this.treeData,result)
      // 删除子级需获取父级id一同删除 否则父级选中状态依旧存在
      result.map(item=>{
        this.checkedKeys=this.checkedKeys.filter(its=>its!=item)
      })
    },
     // 初始化数据处理 通过key 获取title
    getTagsData(data, tags) {
      data.map((item) => {
        this.checkedKeys.map((its) => {
          if (item.key === its) {
            tags.push({
              key: item.key,
              title: item.title,
              isShow: item.children && item.children.length > 0 ? false : true,
            })
          }
        })
        if (item.children) {
          this.getTagsData(item.children, tags)
        }
      })
    },
      // 获取父级id
     getParentId(id, list = [], result = []) {
      for (let i = 0; i < list.length; i += 1) {
        const item = list[i]
        if (item.key === id) {
          result.push(item.key)
          if (result.length === 1) return result
          return true
        }
         // 如果存在下级节点,则继续遍历
        if (item.children) {
           // 预设本次是需要的节点并加入到最终结果result中
          result.push(item.key)
          const find = this.getParentId(id, item.children, result)
           // 如果不是false则表示找到了,直接return,结束递归
          if (find) {
            return result
          }
          // 到这里,意味着本次并不是需要的节点,则在result中移除
          result.pop()
        }
      }
      // 如果都走到这儿了,也就是本轮遍历children没找到,将此次标记为false
      return false
    }
   
  },
}
</script>
<style lang='less' scoped>
.wrapper {
  background: #fff;
  padding: 10px;
}
</style>

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值