[英]Creating nested HTML structures with D3.js
I have an object that looks something like this: 我有一个看起来像这样的对象:
data = {
questions: [
value: "Question 1", answers: [ {value:"answer1"}, {value:"answer2"} ],
value: "Question 2", answers: [ {value:"answer1"}, {value:"answer2"} ]
]
}
(This is simplified - answers have a count that will be displayed using bar graphs. I think I know how to do that once I get there, however.) (这是简化的 - 答案有一个计数,将使用条形图显示。但是,一旦我到达那里,我想我知道如何做到这一点。)
The issue I'm having is dynamically (because the data changes frequently) creating the HTML structure, which should look something like this: 我遇到的问题是动态(因为数据经常更改)创建HTML结构,它应该是这样的:
<div class="question">
Question 1
<div class="answer">Answer 1</div>
<div class="answer">Answer 2</div>
</div>
<div class="question">
Question 2
<div class="answer">Answer 1</div>
<div class="answer">Answer 2</div>
</div>
I can get D3 to create the question divs (the ones w/ class="question"
), and put in the text for that (ie "Question 1", etc.) but I can't figure out how to get D3 to create a child div for the answers (ie the ones w/ class="answer"
). 我可以让D3创建问题div(w class="question"
),然后输入文本(即“问题1”等),但我无法弄清楚如何让D3到为答案创建一个子div(即那些w class="answer"
)。
Here's what I have right now: 这就是我现在所拥有的:
var questions_chart = d3.select('#survey-questions')
.selectAll("div")
.data(data.questions);
questions_chart.transition()
.text(function(d) { return d.value; });
questions_chart.enter().append("div")
.text(function(d) { return d.value; })
.attr("class", "question rounded")
questions_chart.exit().remove();
Basically, how can I nest D3 appends in such a way that I can nest divs
for each answer within the div
for that question? 基本上,我如何嵌套D3附加,以便我可以为该问题的div
每个答案嵌套divs
?
As Lars indicated, you want to use nested selections for this: 正如Lars所说,您希望使用嵌套选择:
var data = {
questions: [
{value: "Question 1", answers: [ {value:"answer1"}, {value:"answer2"} ]},
{value: "Question 2", answers: [ {value:"answer1"}, {value:"answer2"} ]}
]
};
d3.select("body").selectAll("div")
.data(data.questions)
.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; });
Note: I had to fix your data a little, making data.questions
an array of objects. 注意:我必须稍微修复一下数据,使data.questions
成为一个对象数组。
This extends the answer Peter gave to add the "class" information you requested in your original question, as well as answer your follow-up question in the comment: 这扩展了Peter给出的答案,以便在原始问题中添加您所要求的“课程”信息,并在评论中回答您的后续问题:
var divQ = d3.select("body").selectAll("div")
.data(data.questions);
divQ
.enter()
.append("div") // this creates the question divs
.attr("class","question")
.text(function(d) { return d.value; });
divQ
.exit()
.remove();
var divA = divQ.selectAll("div")
.data(function(d) { return d.answers; });
divA
.exit()
.remove();
divA
.enter()
.append("div") // this creates the nested answer divs
.attr("class","answer")
.text(function(d) { return d.value; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.