Skip to content

Instantly share code, notes, and snippets.

@denisemauldin
Created August 3, 2017 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save denisemauldin/df41a0ec91f0d9697b03651b2238a0a0 to your computer and use it in GitHub Desktop.
Save denisemauldin/df41a0ec91f0d9697b03651b2238a0a0 to your computer and use it in GitHub Desktop.
d3 create table
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<script src="https://unpkg.com/d3@4.10.0/build/d3.min.js"></script>
</head>
<body>
<table class="objecttable">
<thead>
<tr><th>name</th><th>age</th><th>gender</th></tr>
</thead>
<tbody></tbody>
</table>
<style>
.objecttable td {
padding: 8px;
}
</style>
<script>
var matrix = [
{name: "Lee Gai Fun", age: 42, sex: "M"},
{name: "Laia Hamidullah", age: 27, sex: "F" },
{name: "Abraham Mdulla", age: 33, sex: "M" }
];
var tr = d3.select(".objecttable tbody")
.selectAll("tr")
.data(matrix)
.enter().append("tr");
var td = tr.selectAll("td")
.data(function(d, i) { return Object.values(d); })
.enter().append("td")
.text(function(d) { return d; });
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment