单击节点时如何打开子节点的子节点
when click on a node how to open child node of a child
我有一个四节点tree.I想点击第二个节点想直接打开第四个节点。
意思是我想打开子节点的子节点。
我正在尝试,但无法获得。
https://jsfiddle.net/5dsnucue/2/
//Toggle children on click.
function click(d) {
// show is true if children currently hidden
var show = !d.children;
showKids (show, d); // set the visibility of this node's kids
// if on second level node and we want to show children of children
// (don't need to same for hide, hiding intermediate node will do the job)
if (d.depth === 1 && show) {
var arr = d.children || d._children;
// loop through kids and set them to visible
arr.forEach (function(child) {
showKids (true, child);
})
}
update(d);
}
// showKids replaces simple toggle with a show parameter
function showKids (show, d) {
if (!show) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
// blank this line as calling showKids with show=true
// twice in a row will end up nulling .children and ._children
// so keep d._children populated.
//
// Same doesn't apply to the 'if' condition above as we don't
// pursue hiding down the tree.
//d._children = null;
}
}
这让您可以执行以下操作:
- 点击二级节点,显示三级和四级节点
- 再次点击2级节点,隐藏3级和4级节点
- 再次点击二级节点,显示三级和四级节点
- 点击第3级节点,第4级节点隐藏
- 点击2级节点,隐藏3级节点
- 再次点击二级节点,显示三级和四级节点
上面的代码可能看起来比实际需要的要复杂一些,但是通过简单的切换步骤 6 会 return 到步骤 4 的状态 - 即仅显示第 3 级。代码中的逻辑确保在展开第 2 级节点时始终显示第 4 级节点,而不管它们之前的状态如何。
我有一个四节点tree.I想点击第二个节点想直接打开第四个节点。 意思是我想打开子节点的子节点。 我正在尝试,但无法获得。
https://jsfiddle.net/5dsnucue/2/
//Toggle children on click.
function click(d) {
// show is true if children currently hidden
var show = !d.children;
showKids (show, d); // set the visibility of this node's kids
// if on second level node and we want to show children of children
// (don't need to same for hide, hiding intermediate node will do the job)
if (d.depth === 1 && show) {
var arr = d.children || d._children;
// loop through kids and set them to visible
arr.forEach (function(child) {
showKids (true, child);
})
}
update(d);
}
// showKids replaces simple toggle with a show parameter
function showKids (show, d) {
if (!show) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
// blank this line as calling showKids with show=true
// twice in a row will end up nulling .children and ._children
// so keep d._children populated.
//
// Same doesn't apply to the 'if' condition above as we don't
// pursue hiding down the tree.
//d._children = null;
}
}
这让您可以执行以下操作:
- 点击二级节点,显示三级和四级节点
- 再次点击2级节点,隐藏3级和4级节点
- 再次点击二级节点,显示三级和四级节点
- 点击第3级节点,第4级节点隐藏
- 点击2级节点,隐藏3级节点
- 再次点击二级节点,显示三级和四级节点
上面的代码可能看起来比实际需要的要复杂一些,但是通过简单的切换步骤 6 会 return 到步骤 4 的状态 - 即仅显示第 3 级。代码中的逻辑确保在展开第 2 级节点时始终显示第 4 级节点,而不管它们之前的状态如何。