{
const parentDiv = d3.create("div").style("padding", "1em").style('overflow', 'auto');
const table = parentDiv.append("table").classed("table-style", true);
const header = table.append("thead");
const body = table.append("tbody");
drawTable(subset, columns);
function drawTable(dataset, cols) {
const headerRow = header.selectAll("th")
.data(cols)
.join("th")
.text(d => d.sorted ? `${d.title} - ${d.sorted.substring(0,1).toUpperCase()}` : d.title)
.attr("class", d => d.sorted ? d.sorted : "");
const tableRows = body.selectAll("tr")
.data(dataset)
.join("tr")
const cells = tableRows.selectAll("td")
.data(d => Object.entries(d))
.join("td").text(d => d[1])
headerRow.on("click", function(e, d,i) {
columns.filter(s => s !== d).forEach(col => col.sorted = null);
if (d.sorted !== "asc") {
let newData = dataset.sort((a,b) => {
return a[d.title] < b[d.title] ? -1 : 1
});
d.sorted = "asc";
drawTable(newData, columns);
} else if (d.sorted === "asc") {
let newData = dataset.sort((a,b) => a[d.title] > b[d.title] ? -1 : 1);
d.sorted = "dsc";
drawTable(newData, columns);
}
})
}
return parentDiv.node()
}