JS递归禁用tree本级及子孙级

主方法:
/**
     * @ item: 机构树的数据
     * @ state:是否禁用的转态
     * @ this.defaultValue: 需要禁用的节点ID
     ************************************/
    init (item, state) {
      item.forEach(x => {
        // 满足条件
        if (x.id === this.defaultValue || state) {
          this.$set(x, 'disabled', true)
          if (x.child && x.child.length) {
            this.init(x.child, true)
          }
          return
        }
        // 不满足条件
        this.$set(x, 'disabled', false)
        if (x.child && x.child.length) {
          this.init(x.child, false)
        }
      })
    }
完整代码:
<template>
  <div class="home">
    <el-input v-model="defaultValue" placeholder="请输入内容" style="margin-bottom: 30px;"></el-input>
    <el-tree :data="data" :props="defaultProps" show-checkbox node-key="id" default-expand-all></el-tree>
  </div>
</template>

<script>
export default {
  name: 'Home',
  components: {
  },
  data () {
    return {
      data: [
        {
          "id": "1772917371990241282",
          "name": "海南省",
          "parentId": null,
          "creatorId": "admin",
          "catalogue": "label",
          "createTime": "2024-03-27 17:23:35",
          "count": "0",
          "child": [
            {
              "id": "1772917598923059202",
              "name": "海口市",
              "parentId": "1772917371990241282",
              "creatorId": "admin",
              "catalogue": "label",
              "createTime": "2024-03-27 17:24:29",
              "count": "1",
              "child": [
                {
                  "id": "1772917766711996418",
                  "name": "秀英区",
                  "parentId": "1772917598923059202",
                  "creatorId": "admin",
                  "catalogue": "label",
                  "createTime": "2024-03-27 17:25:09",
                  "count": "0",
                  "child": [
                    {
                      "id": "1772918158208331778",
                      "name": "东山镇",
                      "parentId": "1772917766711996418",
                      "creatorId": "admin",
                      "catalogue": "label",
                      "createTime": "2024-03-27 17:26:43",
                      "count": "0",
                      "child": []
                    },
                    {
                      "id": "1772918200059097090",
                      "name": "永兴镇",
                      "parentId": "1772917766711996418",
                      "creatorId": "admin",
                      "catalogue": "label",
                      "createTime": "2024-03-27 17:26:53",
                      "count": "0",
                      "child": []
                    },
                    {
                      "id": "1772918302081347586",
                      "name": "遵谭镇",
                      "parentId": "1772917766711996418",
                      "creatorId": "admin",
                      "catalogue": "label",
                      "createTime": "2024-03-27 17:27:17",
                      "count": "1",
                      "child": []
                    }
                  ]
                },
                {
                  "id": "1772917790862798849",
                  "name": "龙华区",
                  "parentId": "1772917598923059202",
                  "creatorId": "admin",
                  "catalogue": "label",
                  "createTime": "2024-03-27 17:25:15",
                  "count": "0",
                  "child": []
                },
                {
                  "id": "1772917816104120322",
                  "name": "美兰区",
                  "parentId": "1772917598923059202",
                  "creatorId": "admin",
                  "catalogue": "label",
                  "createTime": "2024-03-27 17:25:21",
                  "count": "0",
                  "child": []
                },
                {
                  "id": "1772917847217467394",
                  "name": "琼山区",
                  "parentId": "1772917598923059202",
                  "creatorId": "admin",
                  "catalogue": "label",
                  "createTime": "2024-03-27 17:25:29",
                  "count": "1",
                  "child": []
                }
              ]
            },
            {
              "id": "1772917681890586626",
              "name": "三亚市",
              "parentId": "1772917371990241282",
              "creatorId": "admin",
              "catalogue": "label",
              "createTime": "2024-03-27 17:24:49",
              "count": "0",
              "child": []
            }
          ]
        },
        {
          "id": "1774694923257942018",
          "name": "广东省",
          "parentId": null,
          "creatorId": "admin",
          "catalogue": "label",
          "createTime": "2024-04-01 15:06:57",
          "count": "0",
          "child": []
        }
      ],
      defaultProps: {
        children: 'child',
        label: 'name'
      },
      defaultValue: '1772918158208331778'
    }
  },
  watch: {
    defaultValue () {
      this.init(this.data, false)
    }
  },
  mounted() {
    this.init(this.data, false)
  },
  methods: {
    /**
     * @ item: 机构树的数据
     * @ state:是否禁用的转态
     * @ this.defaultValue: 需要禁用的节点ID
     ************************************/
    init (item, state) {
      item.forEach(x => {
        // 满足条件
        if (x.id === this.defaultValue || state) {
          this.$set(x, 'disabled', true)
          if (x.child && x.child.length) {
            this.init(x.child, true)
          }
          return
        }
        // 不满足条件
        this.$set(x, 'disabled', false)
        if (x.child && x.child.length) {
          this.init(x.child, false)
        }
      })
    },
  }
}
</script>

<style lang="scss">
.home {
  width: 100%;
  height: 100%;
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值