Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js prepend (similar to jQuery prepend)

I like the usage of append in D3, and I'm looking for prepend.

Does this exist in D3?

like image 442
Mia Avatar asked Oct 07 '14 11:10

Mia


People also ask

Is D3 similar to jQuery?

D3 is data-driven but jQuery is not: with jQuery you directly manipulate elements, but with D3 you provide data and callbacks through D3's unique data() , enter() and exit() methods and D3 manipulates elements. D3 is usually used for data visualization but jQuery is used for creating web apps.

What is prepend in jQuery?

The prepend() method inserts specified content at the beginning of the selected elements. Tip: To insert content at the end of the selected elements, use the append() method.

What is the use of prepend ()?

prepend() The Element. prepend() method inserts a set of Node objects or string objects before the first child of the Element . String objects are inserted as equivalent Text nodes.

What is append prepend?

prepend() .append() puts data inside an element at the last index; while. . prepend() puts the prepending element at the first index.


2 Answers

You can use

selection.insert(newElement[, anotherExistingElement]) 

For example:

selection.insert("div",":first-child") 

The above code will insert a div before the first child of selected element. Check documentation to learn more.

Another possible way of inserting elements before any node (including plain texts):

var parentEl = d3.select("div").node();  parentEl.insertBefore(document.createElement("div"), parentEl.childNodes[0]);
<script src="https://d3js.org/d3.v3.min.js"></script>  <div>    This is a plain text    <a></a>  </div>
like image 85
Gilsha Avatar answered Nov 08 '22 05:11

Gilsha


Selection.lower()

selection.lower() will place an element as the first child of its parent.

Together with d3's append, selection.append().lower() can replicate jQuery's prepend

Since D3 v4+, D3 has both selection.raise() and selection.lower() methods. These are used most frequently to move elements in an SVG so that certain elements appear overtop of others, where ordering of SVG elements in the DOM determines draw order. But, they can be used for any element in the DOM.

Here's a quick demonstration using divs and paragraphs:

var div = d3.select("div");    div    .append("p")    .text("Inserted")    .lower();    console.log(div.html());
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>  <div id="div">  Text  <p> Child Paragraph </p>  </div>

The snippet takes a div with the following contents:

Text <p> Child Paragraph </p> 

And uses d3 to append a new paragraph and then lower it so that the structure is as follows:

<p>Inserted</p> Text <p> Child Paragraph </p> 

And for comparison with with jQuery's prepend:

var div = $("div");    div    .prepend("<p>Inserted</p>");    console.log(div.html());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  <div id="div">  Text  <p> Child Paragraph </p>  </div>

More Info

The selection.lower() is implemented as such (see docs for more info):

selection.each(function() {   this.parentNode.insertBefore(this, this.parentNode.firstChild); }); 
like image 44
Andrew Reid Avatar answered Nov 08 '22 06:11

Andrew Reid