var data = [{ "label": "chocolate", "text": "Chocolate Cookie", "img": "chocolate.jpg" },
{ "label": "sugar", "text": "Sugar Cookie", "img": "sugar.jpg" }];
d3.select("body").selectAll("div")
.data(data)
.enter().append("div")
.attr("id", function(d) { return d.label; })
.each(function(d) {
d3.select(this).append("p")
.text(d.text);
d3.select(this).append("img")
.attr("src", d.img);
});var g = d3.select("body").selectAll("div")
.data(data);
g.enter().append("div")
.attr("id", function(d) { return d.label; })
.each(function(d) {
d3.select(this).append("text")
.text(d.text1);
d3.select(this).append("text")
.text(d.text2);
}); g.attr("id", function(d) { return d.label; })
.each(function(d) {
//update text?
});
var divs = d3.select("body").selectAll("div")
.data(data)
.enter().append("div")
.attr("id", function(d) { return d.label; })
divs.append("text").text(function(d) { return d.text1 }).classed("t1", true);
divs.append("text").text(function(d) { return d.text2 }).classed("t2", true);
var update = d3.select("body").selectAll("div")
.data(newdata)
update.select("text.t1").text(function(d) { return d.text1 })update.select("text.t2").text(function(d) { return d.text2 })
d3.select("body").selectAll("div") .data(data) .enter().append("div") .attr("id", function(d) { return d.label; })
A functional programmer might not agree.
If each works for update then its at least an option,
-Alex
Each(
D3.select(this).select("text.firstclass").text(...d.text1)
D3.select(this).select("text.firstclass").text(...d.text1)
)
But then I noticed that you hadn't identified your text-elements by class.
It's not necessary to do that since you should be able to address by position, but:
Since both of your sub elements are text you might also consider removing the each from the enter and handling it in the update, using select all:
textU=update.selectAll("text")
.data(function(d){return
[d.text1,d.text2]}).enter()
textU.enter().append("text")
textU.text(function(d){return(d)})
-Alex
By convenience I meant less typing, you are basically reselecting stuff you already have selected by doing it that way. Not sure what it has to do with functional programming.
As for update/exit your text element subselections can behave the same the same way as your group elements. Meaning if you use alex's suggestion of var texts = g.selectAll("text").data([d.text1,d.text2])
You can do enter/update/exit pattern on the selection saved in the texts variable.
// DATA JOIN var divs = d3.select("body").selectAll("div") .data(data); // ENTER // create the elements and set attributes not related to bound data divs.enter().append("div") .each(function() { var s = d3.select(this); s.append("p"); s.append("img"); }); // ENTER + UPDATE // set attributes that are driven by data divs
.attr("id", function(d) { return d.label; })
.each(function(d) { // 'd' is datum from parent div
var s = d3.select(this);
s.select("p")
.text(d.text);
s.select("img")
.attr("src", d.img);
});
// EXIT divs.exit().remove();
// DATA JOIN var divs = d3.select("body").selectAll("div") .data(data); // UPDATE OLD // ENTER // create the elements and set attributes not related to bound data var divsenter = divs.enter().append("div"); divsenter.append("p"); divsenter.append("img"); // ENTER + UPDATE // set data related attributes divs.attr("id", function(d) { return d.label; }); divs.select("p") // 'select' copies data from parent div to child p (not relevant in this case because datum is an object) .text(function(d) { return d.text; }); divs.select("img") .attr("src", function(d) { return d.img; }); // EXIT divs.exit().remove();