Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

antd树形穿梭框有bug,使用默认的非checkStrictly模式,选中父节点,子节点无法取消选择 #18863

Closed
1 task
LastStranger opened this issue Sep 17, 2019 · 6 comments
Assignees
Labels

Comments

@LastStranger
Copy link

  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

Edit on CodeSandbox

Steps to reproduce

点击父节点,子节点被选中,但再次点击子节点的时候,无法取消子节点的选择状态,

What is expected?

取消子节点的选择状态

What is actually happening?

没有任何反应,数据已经改变了,但是UI视觉是没有改变的

Environment Info
antd 3.23.3
React 16.7
System win10
Browser 76

我仅仅是将https://ant.design/components/transfer-cn/#header里面的树形穿梭框demo里面的checkStrictly去掉,就有这个问题了

@afc163 afc163 added the 🤔 Need Reproduce We cannot reproduce your problem label Sep 17, 2019
@yoyo837
Copy link
Contributor

yoyo837 commented Sep 17, 2019

ref: #18753

@LastStranger
Copy link
Author

@LastStranger
Copy link
Author

这个问题困扰了我一整天,codesandbox展示的仅仅是antd官网里面的树形穿梭框demo,我仅仅是将checkStrictly去掉,就导致父节点点击以后,所有的子节点无法取消选择状态, 然而这时候再点击父节点后,子节点又能选择和取消选择了.

@afc163 afc163 removed the 🤔 Need Reproduce We cannot reproduce your problem label Sep 18, 2019
@ant-design ant-design deleted a comment from ant-design-bot Sep 24, 2019
@zombieJ
Copy link
Member

zombieJ commented Sep 29, 2019

对于非 checkStrictly 模式的 Tree,你需要 onItemSelect 时把子节点对应的父节点也取消点,因为父子节点勾选状态是关联的。

@zombieJ zombieJ added the Usage label Sep 29, 2019
@ant-design-bot
Copy link
Contributor

Hello @LastStranger, we use GitHub issues to trace bugs or discuss plans of Ant Design. So, please don't ask usage questions here. You can try to ask questions on Stack Overflow or Segment Fault, then apply tag antd and react to your question.

你好 @LastStranger,Ant Design Issue 板块是用于 bug 反馈与需求讨论的地方。请勿询问如何使用的问题,你可以试着在 Stack Overflow 或者 Segment Fault 中提问(记得添加 antdreact 标签哦~)。

@JasonXiang2014
Copy link

JasonXiang2014 commented Apr 28, 2024

遇到类似的问题, 需要从一个已经存在列表中移除选中的元素, 此选中的元素的父亲节点是选中状态, 非checkStrictly模式, 通过 const [checkedKeys, setCheckedKeys] = useState([])来维护选中的状态。 当清空子节点对应的checkedKey 需要将父节点的选中状态也清空,否则UI无法生效。
我们要做的就是在取消子节点选中状态的同时,也要取消父节点的选中状态,以下是文心一言生成的递归函数。

` const findParentIds = (tree, targetId) => {
let parentIds = []

// 辅助函数:通过id查找树中的节点
function findNodeById(nodes, id) {
  // 遍历节点数组
  for (const node of nodes) {
    // 如果当前节点的id匹配目标id,则返回该节点
    if (node.id === id) {
      return node
    }
    // 递归地在当前节点的children数组中查找
    const result = findNodeById(node.children, id)
    if (result) {
      return result
    }
  }
  // 如果没有找到匹配的节点,返回null
  return null
}

// 辅助函数:递归向上查找父节点,并收集它们的id
function findParents(node) {
  if (!node) {
    // 如果没有节点,则停止查找
    return
  }
  // 先将当前节点的id添加到列表中
  parentIds.push(node.id)
  // 查找当前节点的父节点
  const parentNode = findNodeById(tree, node.pid)
  // 递归查找父节点的父节点
  findParents(parentNode)
}

// 找到目标节点
const targetNode = findNodeById(tree, targetId)
if (targetNode) {
  // 从目标节点开始向上查找父节点,包括根节点
  findParents(targetNode)
}

return parentIds

}
`
此函数的目的是找到当前节点的所有父节点ids数组,从 checkedKeys 移除 父节点数组包含的keys,重新setCheckedKeys即可。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants