Skip to content

Conversation

dive2Pro
Copy link
Contributor

@dive2Pro dive2Pro commented Nov 8, 2018

fix #12383

bug 原因:

异步的情况下会执行 doCreateChildren , 如果刚好children 的前两个或多个是 default-checked-keys 中设置的, 那么会将 parent.checked 设置为 true.

callback 中 会执行 setChecked(true, true), 将所有的 children 的 check 的设置为true

解决办法:
在 执行 callback 之前, 再次检查该节点的 children.


如果需要的话 可以添加 tests, 但是我碰到有个问题, 在 windows 环境下 测试跑不通

@element-bot
Copy link
Member

element-bot commented Nov 8, 2018

Deploy preview for element ready!

Built with commit e5bd430

https://deploy-preview-13349--element.netlify.com

@ziyoung
Copy link
Contributor

ziyoung commented Nov 8, 2018

异步的情况下会执行 doCreateChildren , 如果刚好children 的前两个或多个是 default-checked-keys 中设置的, 那么会将 parent.checked 设置为 true.

调用 doCreateChildren 之后,会调用 insertChild。改变 parent.checked 的那段代码在哪里?

@dive2Pro
Copy link
Contributor Author

dive2Pro commented Nov 8, 2018

这一行 , 最终会执行到 第 402 行, 当第二个刚好属于defaultChecked 时 会将 parent.checked 设置为true

@ziyoung
Copy link
Contributor

ziyoung commented Nov 9, 2018

@dive2Pro 我看了好久,这样改可行的。但是我觉得有些问题存在。

出现 bug 的原因是:

异步的情况下会执行 doCreateChildren , 如果刚好children 的前两个或多个是 default-checked-keys 中设置的, 那么会将 parent.checked 设置为 true.

按照这样的说法,那么 id 为 2 的节点(checked 为 true)刚刚创建时,调用 reInitChecked 方法计算出此父节点的 checked 该为 true 才对。但是在这里 https://jsfiddle.net/8o7gdvhw/ 父元素并没有被选中。

reInitChecked(parent);

在一行,你可以加上 console.log(parent.childNodes.length); 查看一下。新的节点刚插入时,childNodes 还未创建。这里的代码好像还存在一些问题。

@dive2Pro
Copy link
Contributor Author

dive2Pro commented Nov 9, 2018

按照这样的说法,那么 id 为 2 的节点(checked 为 true)刚刚创建时,调用 reInitChecked 方法计算出此父节点的 checked 该为 true 才对。但是在这里 https://jsfiddle.net/8o7gdvhw/ 父元素并没有被选中。

这是因为在第一个 是 defaultChecked 时, 执行 reInitChecked 是 在

child = new Node(child);
constructor 中执行的, 此时的 parent.childNodes 还没有加上刚才创建的 child . 所以在
if (node.childNodes.length === 0) return;

执行的判断, 不会将parent.checked 置为 true

@dive2Pro
Copy link
Contributor Author

dive2Pro commented Nov 10, 2018

当 前两个child 都为 defaultChecked 时, 第二个 child 才会 在调用到 reInitChecked 时, parent.childNodes 只有一个 checked child , 执行下面

const {all, none, half} = getChildState(node.childNodes);

返回的 { all }就会为 true

其他的情况, 这里 all 则正常返回 false

@ziyoung
Copy link
Contributor

ziyoung commented Nov 12, 2018

@dive2Pro 嗯,代码没什么问题了。测试用例精简一下就可以合并了。😄

@shqpeng
Copy link

shqpeng commented Nov 13, 2018

大佬们,这个bug什么时候会更新啊

@ziyoung ziyoung merged commit d97555b into ElemeFE:dev Nov 13, 2018
ziyoung added a commit to ziyoung/element that referenced this pull request Mar 26, 2019
@ziyoung ziyoung mentioned this pull request Mar 26, 2019
3 tasks
iamkun pushed a commit that referenced this pull request Mar 27, 2019
weisiren168 pushed a commit to weisiren168/element that referenced this pull request Jun 20, 2019
* fix: 12383 Tree in lazy mode set child node 'default-checked-keys', display wrong:All nodes are fulfilled

* 添加测试

* fix code style

* update style
weisiren168 pushed a commit to weisiren168/element that referenced this pull request Jun 20, 2019
lzq4047 pushed a commit to lzq4047/element that referenced this pull request May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug Report] Tree in lazy mode, set child node 'default-checked-keys', display wrong:All nodes are fulfilled
4 participants