Skip to content

.select() method overrides existing data #742

Closed

Description

@nrabinowitz

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

Activity

  1. mbostock commented on Aug 1, 2012

    @mbostock
    Member

    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.

    https://github.com/mbostock/d3/wiki/Selections#wiki-select

    See http://bost.ocks.org/mike/nest/ for more.

  2. nrabinowitz commented on Aug 1, 2012

    @nrabinowitz
    Author

    Ok, thanks for the explanation. It wasn't clear from the wiki documentation that this would replace existing data as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions