A d3.select… equivalent to jQuery.children()

后端 未结 5 1762
感动是毒
感动是毒 2021-01-07 21:03

I\'m using d3 to append some elements on enter() and then update them later. However, the next time i try to select those elements the selection is much larger than the orig

5条回答
  •  迷失自我
    2021-01-07 21:14

    Late to the party, but at least in d3 version 4, selection.selectAll() can take a function whose result is an array that contains the new elements to select based on the selected element in the previous selection:

    var parent = d3.selectAll(".myParentClass");
    var children = parent
        //Convert selection to selection representing the children
        .selectAll(function() { return this.childNodes; })
        //Apply filter to children
        .filter('g')
        ;
    

    The primary benefit of this approach compared to the previous answers are that the selection.data() function will still work correctly. The previously proposed methods, which assign results from a new, separate d3.select() call, do not allow this.

提交回复
热议问题