javascript - a ForEach function to make one tr to three tds and make a dinamic table
问题描述
I need to make a little site that get some text boxes, ('Codigo' 'Nome' 'Documento') put on a array, that I already did, and make a a function with ForEach
to create only one <tr>
to three <td>
I'm just not understand what I'm doing bad
function atualizarTabela() {
var tabelaBody = document.getElementById("corpo-tabela"); // corpo-tabela = tbody I need to clean it first
tabelaBody.innerHTML = "";
registros.forEach(function (registros) { // registros is my array that content the three text boxes
var createTr = document.createElement('tr');
for (var i = 0; i < registros[i]; i++) {
var createTd = document.createElement("td");
createTd.textContent = registro[i];
createTr.appendChild(createTd);
}
});
};
<body>
<table id="clientes">
<thead>
<tr>
<th>Codigo</th>
<th>Nome</th>
<th>Documento</th>
</tr>
</thead>
<tbody id="corpo-tabela">
</tbody>
</table>
<div id="bloco-dados">
<p>Código</p>
<input type="text" id="txtCodigo" name="Código">
<p>Nome</p>
<input type="text" id="txtNome" name="Nome">
<p>Documento</p>
<input type="text" id="txtDocumento" name="Documento"><br>
<input type="button" name="botao" id="btnSalvar" value="Salvar"><br>
<input type="button" name="botao2" id="btnNovo" value="Novo">
</div>
<script src="script.js" type="text/javascript"></script>
</body>
Thank you :)
解决方案
Try removing the white space between function and the argument, and changing the argument to a dummy variable. So
registros.forEach(function(r) {
var createTr = document.createElement('tr');
for (var i = 0; i < r.length; i++) {
let createTd = document.createElement("td");
createTd.textContent = r[i];
createTr.appendChild(createTd);
tabelaBody.appendChild(createTr);
}
});
推荐阅读
- python - 使用 SVM 建模数据集
- python - 如何使用 Python 从 LaTeX 表中提取数据
- python - 为不同图像选择动态图像压缩质量率
- cucumber - Cucumber V4 [io.cucumber] 未识别 @Given/When/Then 并给出错误“无法解析导入 cucumber.api.java.en.When”
- javascript - 如何让我的 Javascript 函数在部分 html 页面上工作?
- gitlab - 我是否需要安装 Docker 才能在 Windows 中安装 gitlab-runner?
- python - 我必须在周期中改变什么?
- swift - alertview textField 返回空字符串
- qt - 不是顶级小部件时如何接收dropEvent?
- image - 来自 picsum 的 533 响应后 Flutter NetworkImage 抛出错误