Elementui tree树形控件 未全选保存时未提交父节点id和回显时子节点因父节点全选问题

一。树形控件半选选中状态如下图

当做权限分配时,使用getCheckedKeys可以获取当前选中的节点(不包含半选中状态节点);

vue2写法 this.$refs.tree.getCheckedKeys(false);

vue3写法 tree.value.getCheckedKeys(false)

只有当子节点全选时才能获取到父节点,然而后端保存时需要父节点,怎么办呢?

解决方法:

vue3 写法:先使用getCheckedKeys()获取全选状态节点,在使用getHalfCheckedKeys()获取到半选状态节点然后使用数据合并concat()得到全选状态和半选状态节点数据

const selectedKeys = tree.value.getCheckedKeys(false)
const halfSelectKeys = tree.value.getHalfCheckedKeys(false)
const selectKeys = selectedKeys.concat(halfSelectKeys)

那么问题又出现了,当数据回显时,半选状态的父节点已存在回显数据中,会全部中子节点如下图

不要着急,解决办法如下:

vue3写法

1。首先清空树选中状态
treeRef.value.setCheckedKeys([])
2.遍历后端返回的的树的数组
selectedAllNodesId.forEach((i) => {
   3.根据返回值获取到当前树的节点所有信息
   const node = treeRef.value.getNode(i)
   4.node.isLeaf 判断当前节点是否为子节点
   if (node.isLeaf) {
     5.如果是子节点就把状态设置为选中状态
     treeRef.value.setChecked(node, true)
   }
})
treeRef.value.setCheckedKeys([])
selectedAllNodesId.forEach((i) => {
   const node = treeRef.value.getNode(i)
   if (node.isLeaf) {
     treeRef.value.setChecked(node, true)
   }
})
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我之前的代码确实没有包含父节点选择子节点全选以及一个父节点子节点没有全选父节点不被选中的功能。以下是更新后的代码,它将实现这些功能: ```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> ``` 现在,每个节点都有一个复选框,当选择父节点,所有子节点会被全选。如果一个父节点子节点没有全部选中,则父节点将不会被选中。你可以根据需要进一步处理所选节点的代码,并将其用 `|` 进行拼接。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值