element-ui Tree之懒加载叶子节点强制设置父级半选效果

效果:

前言:

我们是先只展示一级的,二级的数据是通过点击之后通过服务器获取数据,并不是全量数据直接一起返回回来的。

问题:

当你设置了默认选中的子节点,但是由于刚进入页面此时tree中数据暂是没有这个子节点时,其父节点并不会具有半选效果

issue地址:

Feature Request tree components support setHalfCheckedNodes and setHalfCheckedkeys · Issue #13500 · ElemeFE/element · GitHub

html代码:

html 复制代码
<el-tree
   ref="tree"
   :props="{
      label: 'Name',
      isLeaf: 'IsLeaf',
   }"
   :load="loadNode"
   lazy
   show-checkbox
   :filter-node-method="filterNode"
   node-key="Id"
   :default-checked-keys="defaultCheckedKeys"
   @check-change="handleCheckChange"
></el-tree>

需要回显数据:

javascript 复制代码
selectDoctorInfo: [
        {
          Type: 1,
          RelationId: '68d5d334-4eff-4cf4-8152-fa6a45546dae', // 子级的 id
          OrgId: 'bbdcbc14-290f-43f6-91d9-fd31529dbec3', // 父级的 id
        },
        {
          Type: 1,
          RelationId: '08478d81-9582-4151-9288-fca71beb43fb',
          OrgId: 'bbdcbc14-290f-43f6-91d9-fd31529dbec3',
        },
        {
          Type: 2,
          RelationId: '63daa04f-be13-481a-8e5e-6160fee89203',
          OrgId: '63daa04f-be13-481a-8e5e-6160fee89203',
        },
      ]

核心代码:

javascript 复制代码
onEchoTreeData() {
      // 接口返回已选择的数据  allHospitalList: 一级的全部数据  selectDoctorInfo: 需要回显的数据 
      const selectKeys = [];
      this.selectDoctorInfo.forEach((v) => {
        this.allHospitalList.forEach((s) => {
          if (v.Type === 1) {
            if (s.Id === v.OrgId) {
              selectKeys.push(v.RelationId);
              this.$nextTick(() => {
                var node = this.$refs.tree.getNode(v.OrgId); // 拿到父级的 id
                if (node) {
                  // 这里是核心代码
                  node.indeterminate = true // indeterminate强制设置为半选
                }
              })
            }
          }
          if (v.Type === 2) {
            if (s.Id === v.RelationId) {
              selectKeys.push(v.RelationId);
            }
          }
        });
      });
      this.defaultCheckedKeys = selectKeys;
    },
相关推荐
仨孩子的爹1 小时前
Lodop、hiprint 与 vue-print-designer:打印控件选型对比
vue.js
ruanno_top4 小时前
四个 canvas 组件,在 2 倍屏上一直是糊的
vue.js
ruanno_top5 小时前
一个脏数据能让整条折线消失:图表组件的四种数据契约
vue.js
FlyWIHTSKY1 天前
vue2和vue3有什么区别,用法上有哪些区别
前端·javascript·vue.js
ruanno_top1 天前
能点,但 Tab 不到:两个组件把「可操作」做了一半
vue.js
爱吃烧烤的技术渣1 天前
别再手写 CRUD 了:我把 Element Plus 的工作量压缩成一份列 schema
前端·vue.js
徐小夕1 天前
万字长文!DeepSeek Super-agent × 企业知识库架构深度拆解与落地实践
前端·vue.js·github
ruanno_top2 天前
倒计时该用 deadline 还是每秒减一?我把两种写法都跑了一遍
vue.js
徐小夕2 天前
下载量超 30 万的开源智能体 Prime Agent:它把自己从 TypeScript 重写成了 Rust
前端·vue.js·github
toooooop82 天前
CRMEB 故障排查笔记:安全网关只放行 GET/POST 怎么办
前端·javascript·vue.js·thinkphp6·crmeb