2

我正在使用可折叠的树从上到下定向。在这里,我遇到了一些问题。使用 d3.v3.js 实现树。如何将复选框附加到每个节点的树中。

    // Create the link lines.
    svg.selectAll(".link")
        .data(links)
      .enter().append("path")
        .attr("class", "link")
        .attr("d", d3.svg.diagonal().projection(function(d) { return [o.x(d)+15, o.y(d)]; }));

        svg.selectAll("input")
        .data(nodes)
        .enter().append("foreignObject")
        .attr('x' , o.x)
        .attr('y',  o.y)
        .attr('width', 50)
        .attr('height', 20)
        .append("xhtml:body")
        .html("<form><input type=checkbox id=check></input></form>")
     .on("click", function(d, i){
            console.log(svg.select("#check").node().checked) 
            }) ;

    svg.selectAll("image")
        .data(nodes)
     .enter().append("image")
        .attr('x' , o.x)
        .attr('y',  o.y)
        .attr('width', 30)
        .attr('height', 20)
        .attr("xlink:href", "check.png")
  }); 
});

附加到 svg 但在浏览器中不可见的复选框。任何人都请帮我解决这个问题

4

3 回答 3

3

您需要为foreignObjects(即复选框)创建一个持有者,而不是此行中的输入:

svg.selectAll("input")

应该是

svg.selectAll("foreignObject")

然后,您需要使用数据驱动复选框的数量,data(data)并将其与 绑定enter(),就像您所做的那样。如果要出现多个元素,则需要对 x 和 y 使用动态变量。在一个简单的例子中,就是.attr('x', function (d,i) { return d.x; }). 所以把它们放在一起,你就得到了这个

于 2013-10-09T08:59:22.190 回答
1

您的复选框不会出现,因为您不能将任意 html 元素附加到 svg。您应该使用 < g > 或浮动元素:

#canvas {
  position: relative;
}

然后使用以下方法附加复选框元素:

d3.select("#canvas")
  .append("input")
  .attr("type", "checkbox")
  .style("position", "absolute")
  .style("top", "320")
  .style("left", "150")
于 2013-10-09T07:02:58.843 回答
0

这个答案对那些使用Bootstrap的人更有用。要在 bootstrap 中附加复选框,我们需要在附加外来对象时指定标签跨度。否则在浏览器中不可见

 svg.selectAll("foreignObject")
   .data(nodes).enter()
   .append("foreignObject")
   .attr('x', o.x)
   .attr('y',  o.y)
   .attr('width', 30)
   .attr('height', 20)
   .append("xhtml:tree")
   .html("<label class='inline'><input type='checkbox'><span class='lbl'> </span>               </label>");
于 2013-10-09T11:28:10.770 回答