1

The move_to_last callback shown below has as its net effect to make this the last one among all its siblings in the DOM (this is expected to be a DOM element). It does this by first detaching this from the DOM, and then re-appending it to its original parent.

function move_to_last () {
  var n = jQuery(this);
  n.parent().append(n.detach());
}

The intended use of this function is something like

jQuery(".someclass").each(move_to_last);

How would one define the function move_to_last_d3 such that the expression

d3.selectAll(".someclass").each(move_to_last_d3);

would have the same effect as that of the previous expression?

I see an append method in the d3.js API reference page, but I can't spot anything like detach. So this question may boil down to what is the d3.js-equivalent of jQuery's detach method.

kjo
  • 33,683
  • 52
  • 148
  • 265
  • 1
    There's no equivalent, but you should be able to run the D3 code you've posted with your implementation of `move_to_last`. – Lars Kotthoff Oct 04 '13 at 15:01
  • @LarsKotthoff: thanks! (I'll gladly accept it if posted as an answer.) – kjo Oct 04 '13 at 17:29
  • @LarsKotthoff there is a problem with this approach if you try to rebind the data without using a data function (i.e. the DOM will be out of sync with what D3 thinks is there). See related discussion [here](http://stackoverflow.com/questions/14167863/how-can-i-bring-a-circle-to-the-front-with-d3) and [here](http://stackoverflow.com/questions/13595175/updating-svg-element-z-index-with-d3) – explunit Oct 04 '13 at 18:38
  • Just to clarify that last comment. D3 doesn't save the state of the DOM. Its matching is stateless. The point is that when using the default matching function (which relies on the index within an array), different data elements will be matched when reordering the DOM elements. – Lars Kotthoff Oct 04 '13 at 18:47

1 Answers1

1

D3 doesn't provide this functionality itself, but it works nicely together with JQuery. You should be able to run the code as you posted it.

Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204