官术网_书友最值得收藏!

What exactly did we do here?

The key is in the three for...each statements that we used. One loops through the array of table header strings, and appends a table cell (td) element with each value into the thead row. There are then two nested .forEach() statements that do the same for each row in the body. We technically only have one row in the body right now, so probably didn't need that messy double for...each, but now all we have to do to add another row to the table is simply append another data array to the rows variable. We'll talk more about Array.prototype.forEach and other array functions in the next chapter.

This might seem like a lot of work for such a simple table, but the advantages of doing it this way are huge. Instead of wasting a bunch of time typing out a totally static table that you'll never use again, you've effectively created a basic JavaScript library that will produce a basic table for you whenever you need it. You could even extend your tableFactory function to do different things than it does now, without ever altering the code you just wrote.

Those nested for...each loops are really ugly though. Thankfully, d3-selection provides a mechanism for adding, updating, and removing data points, which we'll get into really quickly. Instead of tracking where everything is in your project, we just need to tell D3 to manage our data and it will update any attached elements. The key part of this is selections and building functions that intelligently manipulate them.

主站蜘蛛池模板: 南充市| 门头沟区| 芦溪县| 盖州市| 虹口区| 广宗县| 海淀区| 双辽市| 江安县| 石门县| 磐安县| 延吉市| 呼伦贝尔市| 丰宁| 漳平市| 漳州市| 兰州市| 乐安县| 萨迦县| 锡林浩特市| 什邡市| 清水县| 庆安县| 潍坊市| 武宣县| 汾阳市| 乐昌市| 汾阳市| 呼和浩特市| 井研县| 沙坪坝区| 金塔县| 呈贡县| 宣汉县| 南丹县| 甘洛县| 屏山县| 比如县| 中牟县| 茶陵县| 海阳市|