如何在Javascript中垂直填充数据单元格
How to populate data cells in Javascript vertically?
我在HTML中有这个(name, value)表,我想在一些计算后填充'value'列。
在加载网页时,'name'列已填满,'value'列仍为空白。
在执行了一些计算之后,我想为与'name'对应的每一行依次填充'value'列。从1 -> n.
我的页面是做什么的:
- 从用户处获取号码(例如3)
- 打印一个3行对的表,value列留空。
- 执行一些计算并将其存储在名为'value'的数组中。
下一步:
我想为存储在'value'数组中的每个元素填充'value'列,顺序地,即从1 -> n。
下面是一些代码:
JavaScript -根据开始收到的数字动态创建表
var playerTable = document.getElementById("player_table");
var player_table_row, player_table_datacell1, player_table_datacell2;
var value;
//no_of_players contains the number of rows
for ( var i = 0; i < no_of_players; i++)
{
//inserts i'th row
player_table_row = playerTable.insertRow(i);
//Insert first datacell for the row
player_table_datacell1 = player_table_row.insertCell( 0 );
//Insert second datacell for the row
player_table_datacell2 = player_table_row.insertCell( 1 );
//Inserts the name for the i'th row in the 'name' column
//players_list is the array containing all the names
player_table_datacell1.innerHTML = players_list[i];
//Leave the second cell i.e. 'value' column blank
player_table_datacell2.innerHTML = "";
value[i] = /* Some computations */
}
表的HTML代码,很简单
<table id = "player_table">
<th>Name</th>
<th>Value</th>
</table>
预期输出:Name Value
A 5
B 2
C 19
我已经提前计算了这些值5,2,19,现在只需要将它们填充到HTML表中。我当前的表是这样的:
Name Value
A
B
C
我假设您的计算需要某种用户输入。这就是为什么,您不能在进行计算后,用Name和计算值向表中添加行。
这里有一种方法。我只是为每个值单元格设置id,并使用id进行更新。希望能有所帮助:)
<body>
<table id = "player_table">
<thead>
<th>Name</th>
<th>Value</th>
</thead>
</table>
<script type="text/javascript">
process();
function process() {
var playerTable = document.getElementById("player_table");
var player_table_row, player_table_datacell1, player_table_datacell2;
var value;
//hard coding for testing purposes
no_of_players = 3;
var players_list = ["A", "B", "C"];
//no_of_players contains the number of rows
for (var i = 0; i < no_of_players; i++) {
//inserts i'th row
player_table_row = playerTable.insertRow(i + 1);
//Insert first datacell for the row
player_table_datacell1 = player_table_row.insertCell(0);
//Insert second datacell for the row
player_table_datacell2 = player_table_row.insertCell(1);
player_table_datacell2.id = "value_cell_" + String(i);
//Inserts the name for the i'th row in the 'name' column
//players_list is the array containing all the names
player_table_datacell1.innerHTML = players_list[i];
//Leave the second cell i.e. 'value' column blank
player_table_datacell2.innerHTML = "";
//value[i] = /* Some computations */
}
value = ["100", "200", "300"]; //hard coding for testing purposes
updateValueColumn(value);
}
function updateValueColumn(value) {
var value_cell;
for (var i = 0; i < value.length; i++) {
value_cell = document.getElementById("value_cell_" + String(i));
value_cell.innerHTML = value[i];
}
}
</script>
</body>
生成的表如下:
<table id = "player_table">
<tr>
<th>Name</th>
<th>Value</th>
</tr>
<tr>
<td>A</td>
<td></td>
</tr>
<tr>
<td>B</td>
<td></td>
</tr>
<tr>
<td>C</td>
<td></td>
</tr>
</table>
您可以使用以下代码将第二列作为数组:
var valueCells = document.querySelectorAll("#player_table tr td:nth-child(2)");
然后在某个循环中为每个单元格设置innerHTML。
valueCells[i].innerHTML = value[i];
相关文章:
- 如何在Highchart中动态填充数据
- 使用jquery在选择框中的选项中填充数据
- 使用jquery、javascript重新填充数据
- 如何从 API 填充数据并加载到下拉列表中
- 通过遍历JSON文件来填充数据列表选项
- 在引导模式中,点击相应的datatables行中的按钮,填充数据
- 当选择District时,PHP将从数据库中填充数据
- 从角度工厂返回数据,返回对象后填充数据
- 高图表 - 从数组填充数据
- 在承诺函数从 pouchdb 填充数据之前触发 Ionic/Angular $watch的问题
- 无法用同位素填充数据
- 使用 php 发送 json 填充数据表
- JQuery - 在表单中填充数据的正确方法
- 翡翠选择字段填充数据
- C# MVC 填充数据表中的下拉列表组
- 自动填充数据被javascript的onchange事件覆盖
- 如何在html中只使用ng repeat来填充数据,而不使用repeat html's元素
- 用Firebase和Angular填充数据表
- 通过JS Ajax调用问题填充数据
- 使用两个ajax调用在两个html表中填充数据