Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active February 9, 2016 01:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mbostock/1541816 to your computer and use it in GitHub Desktop.
Expandable Menu
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<script src="//d3js.org/d3.v3.min.js"></script>
<ul>
<script>
var data = [
{name: "foo", links: ["a", "b", "c"]},
{name: "bar", links: ["d", "e", "f"]}
];
var ul = d3.select("ul");
ul.selectAll("li")
.data(data)
.enter().append("li")
.text(function(d) { return d.name; })
.on("click", expand);
function expand(d) {
d3.select(this)
.on("click", null)
.append("ul")
.selectAll("li")
.data(d.links)
.enter().append("li")
.text(function(d) { return d; });
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment