el-tree懒加载反显节点

<el-tree
  ref="tree"
   lazy
   :data="menus"
   :default-checked-keys="checkedKeys"
   :load="getDatas"
   check-strictly
   show-checkbox
   node-key="id"
   @check="checkChange"
 />
  • 默认选中节点
getChecked(val) {
  this.$refs.tree.setCheckedKeys([])
  // 初始化默认选中的key
  this.checkedKeys= []
  val.forEach(function(data) {
    this.checkedKeys.push(data.id)
  })
  this.$refs.tree.setCheckedKeys(this.checkedKeys) // 赋值选中
}
  • 节点选中状态改变
async checkChange(node) {
  // 获取子节点,包含自己
  const childResp = await getChild(node.id)
  if (this.checkedKeys.indexOf(node.id) !== -1) {
    // 子节点
    for (let i = 0; i < childResp.data.length; i++) {
      const index = this.checkedKeys.indexOf(childResp.data[i])
      if (index !== -1) {
        this.checkedKeys.splice(index, 1)
      }
    }
   // 判断是否有父节点
   if (node.parentId !== 0) {
     const resp = await getChildsTree({ pid: node.parentId })
     const arr = [...this.checkedKeys.values()].filter(el => [...resp.data].some(i => i.id === el))
     if (arr.length === 0) {
       const ind = this.checkedKeys.indexOf(node.parentId)
       if (ind !== -1) {
         this.checkedKeys.splice(ind, 1)
       }
     }
   }
  } else {
    // 子节点
    for (let i = 0; i < childResp.data.length; i++) {
      const index = this.checkedKeys.indexOf(childResp.data[i])
      if (index === -1) {
        this.checkedKeys.push(childResp.data[i])
      }
    }
    // 父节点
    if (node.parentId !== 0) {
      const pNode = this.$refs.tree.getNode(node.parentId)
      pNode.indeterminate = true
      this.checkedKeys.push(node.parentId)
    }
  }
   this.$refs.tree.setCheckedKeys(this.checkedKeys)
 }

不足之处还望指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值