elementUI tree动态渲染编辑时,选择父级时会全选所有的子级(回显时父节点和子节点都会被选中)

效果:

解决方法

         <el-tree

                :data="systemTree"

                show-checkbox

                node-key="code"

                :check-strictly="systemNodeFlag"                

                @check-change="handelCheckChange"

                :props="defaultProps" ref="systemTree">

          </el-tree>

1.设置check-strictly 是一个变量systemNodeFlag 默认值是false

2.拿到接口数据后:

     this.systemNodeFlag = true  //重点:给数节点赋值之前 先设置为true

     this.$nextTick(() => {
        this.checkedSystem//拿到接口数据,筛选出选中状态的数据添加到数组中
        this.$refs.systemTree.setCheckedKeys(this.checkedSystem) //给树节点赋值
        this.checkStrictly = false //重点: 赋值完成后 设置为false
      })

名词解释:

很多同学拿到数据后只是进行了setCheckedNodes赋值,所以会出现父节点和子节点都被选中都情况

check-strictly:在显示复选框的情况下,是否严格的遵循父子不互相关联的做法,默认为 false

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
非常抱歉,我之前的代码确实没有包含父节点选择子节点全选以及一个父节点子节点没有全选父节点不被选中的功能。以下是更新后的代码,它将实现这些功能: ```html <!DOCTYPE html> <html> <head> <style> .tree { list-style-type: none; margin: 0; padding: 0; } .tree-item { margin-left: 20px; } </style> </head> <body> <ul id="tree"></ul> <script> const data = [ {"id": 13, "code": "120000", "parent_id": 11, "name": "天津市", "hierarchy": "|1|11|13|"}, {"id": 12, "code": "100000", "parent_id": 11, "name": "北京市", "hierarchy": "|1|11|12|"}, {"id": 11, "code": "0086", "parent_id": 1, "name": "国", "hierarchy": "|1|11|"}, {"id": 1, "parent_id": 0, "name": "所有区域", "hierarchy": "|1|"} ]; function createTree(data, parentId = 0) { const tree = document.createElement("ul"); for (const item of data) { if (item.parent_id === parentId) { const listItem = document.createElement("li"); listItem.classList.add("tree-item"); const checkbox = document.createElement("input"); checkbox.type = "checkbox"; checkbox.value = item.code; checkbox.addEventListener("change", function() { handleCheckboxChange(this, item.id); }); listItem.appendChild(checkbox); const label = document.createElement("label"); label.innerText = item.name; listItem.appendChild(label); const children = createTree(data, item.id); if (children.children.length > 0) { listItem.appendChild(children); } tree.appendChild(listItem); } } return tree; } function handleCheckboxChange(checkbox, itemId) { const childCheckboxes = document.querySelectorAll(`[data-parent="${itemId}"]`); const parentCheckbox = document.querySelector(`[value="${itemId}"]`); for (const childCheckbox of childCheckboxes) { childCheckbox.checked = checkbox.checked; } if (!checkbox.checked && parentCheckbox) { parentCheckbox.checked = false; } } const treeData = createTree(data); document.getElementById("tree").appendChild(treeData); </script> </body> </html> ``` 现在,每个节点都有一个复选框,当选择父节点,所有子节点全选。如果一个父节点子节点没有全部选中,则父节点将不选中。你可以根据需要进一步处理所选节点的代码,并将其用 `|` 进行拼接。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值