[英]How to add click event on d3.js child of collapsible tree?
I'm using d3.js for collapsible tree structure from here 我正在从这里使用d3.js来折叠树结构
Now my goal is to add a click event on the final child (ie) Son of A or Daughter of A 现在,我的目标是在最后一个孩子(即 A的儿子或A的 女儿 )上添加一个点击事件
For example I need to create an alert box on clicking the childless node. 例如,我需要在单击无子节点上创建一个警报框。
I tried to add a condition within the click function, but it works only on my second click and I don't get it why the alert box is not getting up on my first click. 我试图在click函数中添加一个条件,但是它仅在第二次单击时有效,而我不明白为什么警报框在第一次单击时不会上升。
Could some one explain me why is it so? 有人可以解释一下为什么吗? and what is the right way to do it? 正确的方法是什么?
MY CLICK FUNCTION 我的点击功能
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
if (d.children === null){
alert(d.id);
}
}
update(d);
}
Instead of using 而不是使用
if (d.children === null){
alert(d.id);
}
Use 采用
if (!d.children){
alert(d.id);
}
This is because d.children
would be undefined
but you were checking for null
. 这是因为d.children
是undefined
但是您正在检查null
。 Since undefined
and null
equate to false
!d.children
check will succeed. 由于undefined
和null
等于false
!d.children
检查将成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.