el-tree - 选中父级,子级不选中

组件

<!--
  选中父级,子级不选中
-->
<template>
    <div>
      <el-tree
        :data="data"
        :props="props"
        :check-strictly="checkStrictly"
        ref="tree"
        :node-key="nodeKey"
        @check="getCheckedKeys"
        show-checkbox
        :default-expanded-keys="Expandeds"
        :default-checked-keys="Selecteds">
      </el-tree>
    </div>
</template>
<script>
export default {
  props: {
    /* checkStrictly: {
      type: Boolean,
      default () {
        return false
      }
    }, */
    nodeKey: {
      type: String,
      default () {
        return []
      }
    },
    Selecteds: {
      type: Array,
      default () {
        return []
      }
    },
    Expandeds: {
      type: Array,
      default () {
        return []
      }
    },
    options: {
      type: Array,
      default () {
        return []
      }
    },
    // 树节点配置选项
    props: {
      type: Object,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      TreeData: [],
      AllData: [],
      flag: false,
      checkStrictly: true
    }
  },
  watch: {
    Selecteds () {
      // this.TreeData = this.Selecteds
      let that = this
      this.checkStrictly = true
      this.$nextTick(() => {
        that.TreeData = that.Selecteds
        that.$refs.tree.setCheckedKeys(that.Selecteds)
        // that.checkStrictly = false
      })
    }
  },
  computed: {
    data () {
      return this.options
    }
  },
  methods: {
    orgCheckChange (data, checked, indeterminate) {
    },
    /* 获取当前树的ID值 */
    getCheckedKeys (node, keys) {
      // console.log(node)
      // console.log(keys)
      let that = this
      this.flag = true
      // let checkedKeys = keys.checkedKeys
      // let halfChecked = keys.halfCheckedKeys
      // let checkArr = checkedKeys.concat(halfChecked)
      // this.TreeData = checkedKeys
      // this.AllData = checkArr
      this.setNode(this.$refs.tree.getNode(node.locationId))
      this.$nextTick(function () {
        that.AllData = that.$refs.tree.getCheckedKeys()
      })
    },
    // 递归设置子节点和父节点
    setNode (node) {
      if (node.checked) {
        // 如果当前是选中checkbox,则递归设置父节点和父父节点++选中
        this.setParentNode(node)
      } else {
        // 如果当前是取消选中checkbox,则递归设置子节点全部取消选中
        this.setChildNode(node)
      }
    },
    // 递归设置父节点全部选中
    setParentNode (node) {
      if (node.parent) {
        for (const key in node) {
          if (key === 'parent') {
            node[key].checked = true
            this.setParentNode(node[key])
          }
        }
      }
    },
    // 递归设置子节点全部取消选中
    setChildNode (node) {
      if (node.childNodes && node.childNodes.length) {
        node.childNodes.forEach(item => {
          item.checked = false
          this.setChildNode(item)
        })
      }
    }
  }
}
</script>

<el-form-item label="通行区域:"
                        prop="authority">
            <div class="auth-div-item">
              <zc-tree :nodeKey="nodeKey"
                       :Selecteds="selectedId"
                       :Expandeds="expandedId"
                       ref="authorityTree"
                       :options="authorityData"
                       :props="props"></zc-tree>
            </div>
          </el-form-item>
import ZcTree from '../../../components/acms/ZcTreeByZhaJi'
data () {
    return {
      nodeKey: 'locationId',
      props: {
        children: 'children',
        label: 'locationName',
        id: 'locationId'
      },
      selectedId: [],
      expandedId: [],
      authorityData: [],
      minAuthorityIds: [],
      srcAuthorityIds: [],
    }
},
methods: {
    // 获取区域
    getLocations () {
      AxiosApi(
        '/ifms/web-org/locations',
        {},
        'GET'
      ).then(res => {
        let data = res.data.list || []
        // console.log(data)
        this.authorityData = data
        this.getChild(data)
        this.detailLocations()
        for (let i = 0; i < data.length; i++) {
          // this.expandedId.push(data[i].locationId)
        }
      })
    },
    // 查找子集
    getChild (data) {
      data.forEach((item) => {
        if (item.child && item.child.length > 0) {
          this.getChild(item.child)
        } else {
          this.minAuthorityIds.push(item.locationId)
        }
      })
    },
    // 查看详情
    detailLocations (id, params = {}) {
      AxiosApi(`/acms/web-ac/location-patient/${this.params[this.primaryId]}`, {}, 'GET', params).then(res => {
        let temp = []
        // Object.assign(this.addFormData, res.data)
        // this.srcAuthorityIds = [...this.addFormData.deviceIdList]
        if (res.status === 200) {
          res.data.forEach((item) => {
            this.srcAuthorityIds.push(item.locationId)
            // if (this.minAuthorityIds.toString().includes(item.locationId)) {
            temp.push(item.locationId)
            // }
          })
          this.selectedId = temp
        } else {
          MsgShow('warning', res.desc, 3000)
        }
      })
    },
    // 通过父类获取子类的方法 获取值
    getTreeList (obj) {
      let temp = []
      if (this.$refs.authorityTree.flag) {
        temp = this.$refs.authorityTree.AllData
      } else {
        temp = this.srcAuthorityIds
      }
      return temp
    },
}

{
    "status": 200,
    "desc": "操作成功",
    "data": {
        "list": [
            {
                "locationId": "83b91b2ceab24a37b88badb5ffd25305",
                "locationName": "A区域",
                "locationType": "location",
                "parentId": "",
                "parentType": "",
                "level": 1,
                "children": [
                    {
                        "locationId": "42c3d9fda6cb4a96b90ca3ba48da228d",
                        "locationName": "一栋",
                        "locationType": "building",
                        "parentId": "83b91b2ceab24a37b88badb5ffd25305",
                        "parentType": "location",
                        "level": 2,
                        "children": [
                            {
                                "locationId": "eaba47822f0d45939e0d9ef9e5c6fdb5",
                                "locationName": "一单元",
                                "locationType": "unit",
                                "parentId": "42c3d9fda6cb4a96b90ca3ba48da228d",
                                "parentType": "building",
                                "level": 3,
                                "children": [
                                    {
                                        "locationId": "07360109acc145f3b8417441a024aec1",
                                        "locationName": "楼层一",
                                        "locationType": "floor",
                                        "parentId": "eaba47822f0d45939e0d9ef9e5c6fdb5",
                                        "parentType": "unit",
                                        "level": 4,
                                        "children": [
                                            {
                                                "locationId": "eeb65526258a4345ba5b1e62e0aef31c",
                                                "locationName": "0001室",
                                                "locationType": "house",
                                                "parentId": "07360109acc145f3b8417441a024aec1",
                                                "parentType": "floor",
                                                "level": 5,
                                                "children": null
                                            }
                                        ]
                                    },
                                    {
                                        "locationId": "38aed108499145e188356252dcb12767",
                                        "locationName": "楼层二",
                                        "locationType": "floor",
                                        "parentId": "eaba47822f0d45939e0d9ef9e5c6fdb5",
                                        "parentType": "unit",
                                        "level": 4,
                                        "children": null
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "locationId": "ee54c814b92d4119b3f041cb484c1a89",
                        "locationName": "二栋",
                        "locationType": "building",
                        "parentId": "83b91b2ceab24a37b88badb5ffd25305",
                        "parentType": "location",
                        "level": 2,
                        "children": [
                            {
                                "locationId": "37c466962c684ad5a07978aa6f92950c",
                                "locationName": "二单元",
                                "locationType": "unit",
                                "parentId": "ee54c814b92d4119b3f041cb484c1a89",
                                "parentType": "building",
                                "level": 3,
                                "children": [
                                    {
                                        "locationId": "ae87405f9ee54581b0475f1020fe1662",
                                        "locationName": "楼层一",
                                        "locationType": "floor",
                                        "parentId": "37c466962c684ad5a07978aa6f92950c",
                                        "parentType": "unit",
                                        "level": 4,
                                        "children": [
                                            {
                                                "locationId": "a206ada06d024a0e9fd7861ee5795c95",
                                                "locationName": "0001室",
                                                "locationType": "house",
                                                "parentId": "ae87405f9ee54581b0475f1020fe1662",
                                                "parentType": "floor",
                                                "level": 5,
                                                "children": null
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "locationId": "1abcbd1a4ec5434096f704337ceb127b",
                "locationName": "中心医院区域",
                "locationType": "location",
                "parentId": "",
                "parentType": "",
                "level": 1,
                "children": [
                    {
                        "locationId": "29b11a3e26c74ad184d340622608b59c",
                        "locationName": "南病房楼大门",
                        "locationType": "building",
                        "parentId": "1abcbd1a4ec5434096f704337ceb127b",
                        "parentType": "location",
                        "level": 2,
                        "children": [
                            {
                                "locationId": "d3139a03836e4334acb69b527dad6a47",
                                "locationName": "骨科",
                                "locationType": "unit",
                                "parentId": "29b11a3e26c74ad184d340622608b59c",
                                "parentType": "building",
                                "level": 3,
                                "children": null
                            },
                            {
                                "locationId": "ba570c67141c417f9f867a2e034d457e",
                                "locationName": "心内科",
                                "locationType": "unit",
                                "parentId": "29b11a3e26c74ad184d340622608b59c",
                                "parentType": "building",
                                "level": 3,
                                "children": null
                            }
                        ]
                    },
                    {
                        "locationId": "f76fe3e5aa0242c799a3a486d5143588",
                        "locationName": "北病房楼大门",
                        "locationType": "building",
                        "parentId": "1abcbd1a4ec5434096f704337ceb127b",
                        "parentType": "location",
                        "level": 2,
                        "children": [
                            {
                                "locationId": "e28d5ff604ca4983b687f19420cacbe0",
                                "locationName": "眼科",
                                "locationType": "unit",
                                "parentId": "f76fe3e5aa0242c799a3a486d5143588",
                                "parentType": "building",
                                "level": 3,
                                "children": null
                            }
                        ]
                    },
                    {
                        "locationId": "129ef291a5ac48b4821ac4815099295f",
                        "locationName": "急诊楼",
                        "locationType": "building",
                        "parentId": "1abcbd1a4ec5434096f704337ceb127b",
                        "parentType": "location",
                        "level": 2,
                        "children": null
                    },
                    {
                        "locationId": "ac6d5741b2bc46baa2aa4ceee64f9190",
                        "locationName": "门诊楼2楼北门",
                        "locationType": "building",
                        "parentId": "1abcbd1a4ec5434096f704337ceb127b",
                        "parentType": "location",
                        "level": 2,
                        "children": null
                    }
                ]
            },
            {
                "locationId": "a8f7ff92ab8349cf96e923c92fc4cf0c",
                "locationName": "测试区域",
                "locationType": "location",
                "parentId": "",
                "parentType": "",
                "level": 1,
                "children": [
                    {
                        "locationId": "f007a786fc314fddb36f6b9e1d2cf6dc",
                        "locationName": "一栋",
                        "locationType": "building",
                        "parentId": "a8f7ff92ab8349cf96e923c92fc4cf0c",
                        "parentType": "location",
                        "level": 2,
                        "children": null
                    },
                    {
                        "locationId": "7dc77c7f623d446baabf59ac930f13da",
                        "locationName": "二栋",
                        "locationType": "building",
                        "parentId": "a8f7ff92ab8349cf96e923c92fc4cf0c",
                        "parentType": "location",
                        "level": 2,
                        "children": null
                    }
                ]
            }
        ]
    }
}

{
    "status": 200,
    "desc": "操作成功",
    "data": [
        {
            "locationId": "83b91b2ceab24a37b88badb5ffd25305",
            "locationName": "A区域"
        }
    ]
}
返回文章列表 打赏
本页链接的二维码
打赏二维码
期待你的评论