Javascript 如何填充表
我已经创建了一个表,需要使用JavaScript填充Javascript 如何填充表,javascript,jquery,html,Javascript,Jquery,Html,我已经创建了一个表,需要使用JavaScript填充tds,因为数据来自服务器。 这是我的桌子: <table id="report" class="infoRecurso" id='testExportId' style="width: 100%; color: #363636"> <thead style="color: #363636"> <tr class='btn-danger' style="background-color: lightgray
tds
,因为数据来自服务器。
这是我的桌子:
<table id="report" class="infoRecurso" id='testExportId' style="width: 100%; color: #363636">
<thead style="color: #363636">
<tr class='btn-danger' style="background-color: lightgray; color: #363636">
<th class="thData">Data</th>
<th id="timeIniticial" class="thInicio">Início</th>
<th class="thTermino">Término</th>
<th class="thDuracao">Duração</th>
<th class="thAtividades">Atividades</th>
</tr>
</thead>
<tbody id="myTable">
<tr>
<td style="text-align: left;"></td>>
<td id="inicialTime"></td>
<td>Hora Final</td>
<td style="text-align: left;">Duração</td>
<td colspan="1" class="quebraLinha" style="text-align: left;"></td>
</tr>
<tr>
<th class="horasTotaisTh" colspan="3"> Horas Totais </th>
<th class="resultadoHorasTotaisTh" colspan="4">Hora Total</th>
</tr>
</tbody>
但此代码仅填充第一个位置
我如何填充其余的
tds
职位?您的问题留下了一些悬而未决的问题,因此我只能猜测您真正想要什么。在下面的代码片段中,我编译了一个解决方案,该解决方案将.prepend()
一些html代码添加到您的表中,从而使您的第二列始终由数据数组中的代码填充。我将此
元素的id
属性更改为class
,因为id必须始终是唯一的
我还“更正”了上一个表行的colspan属性,因为总共只有5列
var数据=[{“产品”:“RD0”},{“产品”:“RD1-184”},{“产品”:“RD1-185”}];
var html=data.map({product})=>
`${product}Hora Final
Duração`)。加入(“”)
$('.infoRecurso tbody').prepend(html)代码>
td{text align:left}
资料
伊尼西奥
蒂尔米诺
杜拉ção
阿维达斯
霍拉斯·托泰斯
霍拉总数
ID在页面上是唯一的,如果您需要与多个元素交互,则应使用css类,并使用选择器检索具有类名的所有td,这将为您提供一个节点列表,您可以循环并应用更改。
var data = [{"product": "RD0"}, {"product": "RD1-184"}, {"product": "RD1-185"}];
var table = $('.infoRecurso');
$.each(data, function(i, value) {
table.find('tr').eq(i).find("td[id='inicialTime']").text(value.product);
});