简体   繁体   English

具有D3嵌套选择的enter()和exit()

[英]enter() and exit() with D3 nested selections

I have a follow-up question to this question that I closed too hastily. 对于这个问题,我有一个后续问题,我匆忙解决。

How/where would I put a .exit() or .transition() for the answers? 如何/在那里我会放一个.exit().transition()的答案? In my original code, I'm saving the main chart in a variable ( questions_chart ), so I can say something like questions_chart.exit().remove() . 在我的原始代码中,我将主图表保存在一个变量( questions_chart )中,因此我可以说诸如questions_chart.exit().remove() However, if I put .exit().remove() after the .text() (last line in the code in Peter's answer), I get an error saying that object array has no method 'exit' 但是,如果将.exit().remove()放在.text() (Peter答案中代码的最后一行),则会收到错误消息,指出object array has no method 'exit'

I have not tested this but will give it a shot...you would need to preserve the variable binding the data...like so: 我还没有测试过,但是会试一试...您需要保留绑定数据的变量...就像这样:

var divs = d3.select("body").selectAll("div")
    .data(data.questions);

divs
    .enter().append("div") // this creates the question divs
    .text(function(d) { return d.value; })
    .selectAll("div")
    .data(function(d) { return d.answers; })
    .enter().append("div") // this creates the nested answer divs
    .text(function(d) { return d.value; });

divs.exit().remove();

I am assuming you don't need to remove just the divs that are answers. 我假设您不需要仅删除答案的div。 Hope this helps. 希望这可以帮助。

UPDATE: giving it a shot at dealing with questions and answers... 更新:试着处理问题和答案...

var divs = d3.select("body").selectAll("div")
    .data(data.questions);

var questionDivs = divs
    .enter().append("div") // this creates the question divs
    .text(function(d) { return d.value; });

divs.exit().remove();

var answerDivs = questionDivs
    .selectAll("div")
    .data(function(d) { return d.answers; });

answerDivs
    .enter().append("div") // this creates the nested answer divs
    .text(function(d) { return d.value; });

answerDivs.exit().remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM