【发布时间】:2019-01-22 17:32:36
【问题描述】:
我有一个名为 jogos2018 的变量,它存储两个对象(jogos1 和 jogos2),这两个对象中的每一个都包含多个属性。
我想将两个对象显示为 html 标记 <tr>,并将每个内容显示为一个单元格 <td>。
我怎样才能巧妙地完成这个动作?
我试着把它们都写出来,但是我有超过 50 个对象,所以要花很长时间才能完成。有没有更快的方法来完成它?
<!DOCTYPE html>
<html>
<head>
<style>
tr {
background-color: red;
}
</style>
</head>
<body onload="myFunction()">
<table id="table_calendario_jogos">
<tbody>
<tr id="table_header">
<th>Data</th>
<th>Hora</th>
<th>Oponente</th>
<th>Placar</th>
<th>Torneio</th>
<th>Temporada</th>
<th>Local</th>
<th>Mais</th>
</tr>
</tbody>
<tbody id="tbody_calendario_jogos">
</tbody>
</table>
<script>
var jogos2018 = {
jogo1: {
Data: "11/03/2018",
Hora: "15:00h",
Imagem: "",
Oponente: "BMC",
Placar: "V, 52-42",
Torneio: "LSB",
Temporada: "Regular",
Local: "Ginásio Miécimo da Silva",
Mais: "Mais+"
},
jogo2: {
Data: "13/07/2018",
Hora: "18:00h",
Imagem: "",
Oponente: "UERJ",
Placar: "V, 67-32",
Torneio: "LSB",
Temporada: "Regular",
Local: "Ginásio Miécimo da Silva",
Mais: "Mais+"
}
};
function myFunction() {
var tableRow = document.createElement("TR");
tableRow.setAttribute("id", "jogos2018_TR");
document.getElementById("tbody_calendario_jogos").appendChild(tableRow);
/* Data JOGO 1 - BMC 11/04/2018 */
var tableData = document.createElement("TD");
var tableJogosData = document.createTextNode(jogos2018.jogo1.Data);
tableData.appendChild(tableJogosData);
document.getElementById("jogos2018_TR").appendChild(tableData);
}
</script>
</body>
</html>
【问题讨论】:
-
您可以为此使用库。我的建议是 react-table react-table.js.org/#/story/simple-table
标签: javascript arrays performance html-table