Tree 树形组件详解 default-expand-all 动态失效问题

1.更具给的数据依次配置

const treeRef = ref(null)
const treeData = ref([])

const props = {              //看下他的事例
  value: "id",
  label: "name",        //按照他的属性名,配置自己的值
  children: "child"
}

2.我这边是根据id唯一值来显示选中状态

3.渲染是正常请求接口就能出现

4.主要是操作其中的数据,比如拿到选中状态的数据 ,我这边后台只需要id就可以了

  let permission_ids = []
   permission_ids = treeRef
    .value!.getCheckedKeys(false)
    .filter(score => score < 1000)

//这个方法返回的是选中状态id的数组
 treeRef.value!.getCheckedKeys(false)

//但是要给组件  node-key="id"  这个树形作为唯一值

5.数据的回显

 //操作自己的数据,给这个方法传入你的id[],我这里permission_ids
treeRef.value!.setCheckedKeys(permission_ids, false)

6.树组件的折叠与展开,这个我找了很久csdn和gpt几乎全是错误的

 <el-tree
            style="max-width: 700px; font-weight: 400"
            :height="280"
            :data="dataRole"
            :ref="'treeRef'"
            :props="props"
            show-checkbox
            node-key="id"
            highlight-current
            :default-expand-all="isfalse"
            :key="treeKey"
          />

    //要给组件加一个key,防止dom不渲染更新
    //才能动态展开与折叠 :default-expand-all="isfalse"

   //同时,展开和折叠会导致数据的选中状态丢失,所以要在之前要储存数据如下

   // 动态展示
    const rebellion = async () => {
     permissions()
     treeKey.value = +new Date()
     isfalse.value = !isfalse.value
     await nextTick()
     treeRef.value!.setCheckedKeys(permission_ids, false)
   }

    // 权限ids
    const permissions = () => {
     permission_ids = treeRef
    .value!.getCheckedKeys(false)
    .filter(score => score < 1000)
   }

6_1 回显实现---选中默认展开  

default-expanded-keys:默认展开的节点的 key 的数组

注意--需要在动态在展开折叠中 清空 回显的默认展开,不然会显示不全

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值