Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In d3 for javascript, how do you create different elements for your data?

For example, in a html <table>, a <tr> may contain <th> and <td>. How would you bind data to a row selection that would create even columns as <th> and odd as <td>?

like image 324
Mats Johnson Avatar asked Aug 27 '12 14:08

Mats Johnson


1 Answers

So, this doesn't seem perfect either, but there's always the html() method.

​var d = [['a','b','c','d']];

var r = d3.select('#myTable').selectAll('tr')
    .data(d);

r.enter().append('tr').html(function(d) {
    var i, s = '';
    for (i = 0; i < d.length; i += 1) {
        s += (i%2===0) ? '<th>' : '<td>';
        s += d[i];
        s += (i%2===0) ? '</th>' : '</td>';
    }
    return s;
}​​​​​​​​​​​​​​​);
like image 116
Andrew Avatar answered Oct 03 '22 08:10

Andrew