Skip to content
Advertisement

Creating a table with D3

I’m trying to create a table in D3 that updates with new data. This mostly works.

Somehow I’m not getting any columns in newly added rows (so initially the table is empty).

What am I doing wrong?

See: https://jsfiddle.net/yev4kujn/21/

JavaScript

Advertisement

Answer

In D3 selections are immutable. Therefore, when you do this…

JavaScript

… you are not changing what rows is: it keeps being an update selection, which is empty when the function runs for the first time. So, when you come to…

JavaScript

… there are no <tr>s to append the <td>s.

All that being said, you have to change what rows is:

JavaScript

Now rows contains the entered elements.

Here is your code with that change:

JavaScript
JavaScript
JavaScript
Advertisement