简体   繁体   English

D3。 我如何对所选节点和所有父节点执行操作

[英]D3. How do I act on the selected node and all parent nodes

I have a D3 tree diagram link this one... https://bl.ocks.org/mbostock/4339083 and can select a node fine. 我有一个D3树形图链接这一个... https://bl.ocks.org/mbostock/4339083并可以选择一个节点。 I want to be able to iterate over the parent nodes too, and set an attribute eg d.MainBranch="Yes" . 我也希望能够遍历父节点,并设置一个属性,例如d.MainBranch="Yes" The idea is I can right click, get a menu to pop up and then click "Mark as Main Branch". 我的想法是我可以右键单击,弹出菜单,然后单击“标记为主分支”。 I've done the right click to get a popup menu. 我已经完成了右键单击以获取一个弹出菜单。 and can set d.MainBranch="Yes" on the current node like this... 并可以像这样在当前节点上设置d.MainBranch="Yes" ...

function marAsMainBranch(d) {           
    d.mainBranch="Yes"; 
    update(d);
}

just lacking the ability to iterate over the parents :-( 只是缺乏迭代父母的能力:-(

Can anyone help? 有人可以帮忙吗?

You can do something like this to set parent data and call recursively till no parent (starting from the node which is acted) : 您可以执行以下操作来设置父数据并递归调用直到没有父节点(从被执行的节点开始)

function setInfoParent(d){
  d.mainBranch="Yes";
  if(d.parent){
    setInfoParent(d.parent);//call recursively itself till no parent.
  }
  console.log(d);
}

Then in click or right click action call setInfoParent something like this: 然后在单击或右键单击操作中调用setInfoParent ,如下所示:

function click(d) {
  setInfoParent(d);//call to set all parents.
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);
}

Working fiddle here 在这里工作小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果选择了所有子节点,如何将父树节点标记为选中 - How to mark parent tree node as selected if all the child nodes are selected 如何找到相邻节点和所有选定节点的边缘? D3 - How do I find neighboring nodes & edges of all selected nodes ? D3 如何从 Selenium/Python 的父节点中删除/排除选定的子节点? - How do I delete/exclude selected children nodes from the parent node in Selenium/Python? 如何一次移动所有选定的节点? D3 / JS - How do I move all selected nodes at once ? D3/JS Kendo Treeview-如何在另一个Treeview中显示具有所有父节点的选定节点 - Kendo Treeview - How to Display selected node with all parent nodes in another treeview 在jstree中选择子节点时检查所有父节点 - Check all parent nodes when child node is selected in jstree 如何在 d3.js 中折叠(显示和隐藏)父节点的子节点? - How can I collapse (show and hide) the child nodes of a parent node in d3.js? d3。 根据数组内容更改各个节点的颜色 - d3. change color of nodes individual depending on array content 如何从Firebase中的父节点获取所有子节点? - How can I get all the child nodes from a parent node in Firebase? 在D3树中,如何以编程方式(无需单击节点)关闭选定深度的所有节点(例如,仅孙子节点) - In a D3 tree, how to close all nodes at a selected depth (ex. only grandchildren) programmatically (without clicking on a node)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM