You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using .select() instead of .selectAll() appears to replace child data with parent data. To recreate:
var parent = d3.select('body')
.selectAll('div.test1')
.data(['foo']);
parent.enter().append('div')
.attr('class', 'test1');
var child = parent.selectAll('span.test2')
.data(['bar']);
child.enter().append('span')
.attr('class', 'test2');
When I use .selectAll(), I get the expected child data:
// access child data using .selectAll()
parent.selectAll('span.test2')
.each(function(d) { console.log(d) });
// "bar"
But using .select() squashes child data:
// access child data using .select()
parent.select('span.test2')
.each(function(d) { console.log(d) });
// foo
// the child data has been replaced, even with .selectAll()
parent.selectAll('span.test2')
.each(function(d) { console.log(d) });
// foo
This is the expected (and desired) behavior; this is what allows selection.append and selection.insert to propagate data after a data-join.
If the current element has associated data, this data is inherited by the returned subselection, and automatically bound to the newly selected elements.
Activity
mbostock commentedon Aug 1, 2012
This is the expected (and desired) behavior; this is what allows selection.append and selection.insert to propagate data after a data-join.
https://github.com/mbostock/d3/wiki/Selections#wiki-select
See http://bost.ocks.org/mike/nest/ for more.
nrabinowitz commentedon Aug 1, 2012
Ok, thanks for the explanation. It wasn't clear from the wiki documentation that this would replace existing data as well.