[英]d3 - nested selections/sub-item update issue
I'm getting used to d3, but seem to be having trouble with more advanced structures. 我已经习惯了d3,但是似乎在使用更高级的结构时遇到了麻烦。 I'm fairly sure there's some subtlety or concept I am not fully appreciating. 我很确定我没有完全欣赏其中的一些微妙之处或概念。 I want to be able to mirror a changing hierarchical data structure with a changing hierarchical element structure. 我希望能够用变化的层次结构元素镜像变化的层次结构数据结构。
My data structure is 3 groups, each with 3 items. 我的数据结构是3组,每组3个项目。 Each group and item has a unique key, extracted using a key function in the data()
call. 每个组和每个项都有一个唯一的键,该键是使用data()
调用中的键函数提取的。
I build the structure, and I can remove a top-level item; 我构建了结构,并且可以删除顶级项目; .exit().remove()
works just fine on that selection. .exit().remove()
在该选择上效果很好。 BUT, modifying or removing any sub-item is simply not reflected in the generated element structure. 但是,根本不会在生成的元素结构中反映,修改或删除任何子项目。
Full (non-)working jsFiddle here!: http://jsfiddle.net/eu95R/2/ , and the all-important enticingly beautiful screenshot: 完整的(非)有效jsFiddle此处!: http : //jsfiddle.net/eu95R/2/ ,以及极为重要的诱人的漂亮屏幕截图:
The problem is that your definition of groups
is using svg.enter()
and the subselection is made on groups
. 问题是您对groups
的定义使用svg.enter()
并且对groups
进行了子选择。 That is, you're not seeing a change because groups
in this case is empty (no enter selection for the SVGs) and therefore there's no subselection. 也就是说,您没有看到任何变化,因为在这种情况下groups
是空的(没有为SVG输入选择),因此没有子选择。
To fix, simply do the subselection based on eg svg
(there are a number of ways to fix this -- not saying that this is necessarily the best one). 要解决此问题,只需基于svg
(有很多方法可以解决此问题-并不是说这一定是最好的方法)。 As you are appending the elements to a g
within the SVG, the selector would be svg.selectAll("g").selectAll("text.item")...
. 当您将元素附加到SVG中的g
,选择器将为svg.selectAll("g").selectAll("text.item")...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.