<a-tree
              v-model=”checkedKeys”
              :replaceFields=”replaceFields”
              checkable
              :expanded-keys=”expandedKeys”
              :auto-expand-parent=”autoExpandParent”
              :selected-keys=”selectedKeys”
              :tree-data=”menuTreeData”
              @expand=”onExpand”
              @select=”onSelect”
              @check=”onCheck”
              ref=”tree”
     methods: {
    onExpand(expandedKeys) {
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    onCheck(checkedKeys, info) {
      this.parentNode = info.halfCheckedKeys
      this.menuids = checkedKeys
      this.checkedKeys = checkedKeys
    },
    onSelect(selectedKeys, info) {
      this.selectedKeys = selectedKeys
    }
    //地狱回调
    getTreeChildren(data) {
      data &&
        data.map((item) => {
          if (item.children && item.children.length > 0) {
            this.getTreeChildren(item.children)
            // console.log("这些是父")
          } else {
            // console.log("这些是子")
            this.newArr.push(item.menuId)
          }
          return null
        })
      return this.newArr
    },
    edit(record) {
      getRole(pick(record, ['roleId'])).then((res) => {
        this.subsystemId = res.rows[0].subsystemId
        this.checkedKeys = res.rows[0].menuIds
        // this.checkedKeys = res的减去parent的 得出[子id]
        let params = {}
        params.subsystemId = this.subsystemId
        getListsubsystom(params.subsystemId).then((res) => {
          this.menuTreeData = res.data
          var str = this.getTreeChildren(this.menuTreeData)
          var arr=[]
          for (var i = 0; i < this.checkedKeys.length; i++) {
            var found = str.indexOf(this.checkedKeys[i])
            console.log(found)
            if(found!=-1){
              arr.push(this.checkedKeys[i])
            }
          }
          this.expandedKeys=arr
          this.autoExpandParent = true
          this.checkedKeys=arr
        })
      })
    }

第一段第二段相信大家可以看懂,第三段代码是我根据

params.subsystemId

请求的数据,然后对数据进行了地狱回调,把id全拿出来,判断它的children,没有的话全拿出来,这些就是子节点,然后用这些子节点去查找请求过来的那几个id,找到了就push到数组中,这个数组赋值给checkedKeys这个就好了,

this.expandedKeys=arr这是用来控制初始默认展开的数据
this.autoExpandParent = true使用开解决默认展开的数据不能关闭的问题
 

版权声明:本文为zhuixunni原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhuixunni/p/13995592.html