【发布时间】:2020-12-05 10:55:11
【问题描述】:
背景: 我正在尝试学习javascript,所以我决定尝试制作一个类似电子表格的东西来学习循环。现在我已经断断续续地工作了一个多月,我真的可以使用一些额外的帮助。由于我正在尝试学习 vanilla js,因此我试图远离其他任何事情。
到目前为止: 我找到了用于计算行和整个表的不同解决方案,但我觉得这些解决方案中的任何一个都不是很好。主要是我一直在为此使用 for 循环,但我注意到 foreach 可能会更好,我正在努力学习......所以现在我正在尝试理解嵌套的 foreach 循环。
问题: 由于我将使用表格中不同位置的每个单元格的数据,因此我需要弄清楚如何将每一行与其余行分开,然后将特定行中的每个单元格分开(用于计算列) .
解决方案:
正如我所说,我有一些用于计算行甚至整个表的奇怪解决方案,但它们并没有真正回答我的问题。一个想法,我在这里可能是非常错误的,是以某种方式将每一行变成一个数组。
我知道以前在这里问过类似的问题,并且我已经测试了其中的几个,但我仍然卡住了。
我已将所有代码放在下面的 fiddlejs 中。该表已设置为在更改时工作。
非常感谢任何形式的帮助
https://jsfiddle.net/mpv0ds5g/1/
HTML:
<body>
<div class="container">
<!-- FORM 1 -->
<form>
<table>
<caption>
<strong>FIRST</strong>
</caption>
<tbody>
<!-- ROW 0 -->
<tr>
<th scope="row">Row 0</th>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><output class="rowsum output">0</output></td>
</tr>
<!-- ROW 1 -->
<tr>
<th scope="row">Row 1</th>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><output class="rowsum output">0</output></td>
</tr>
</tbody>
</table>
</form>
<hr />
<!-- FORM 2 -->
<form>
<table>
<caption>
<strong>SECOND</strong>
</caption>
<tbody>
<!-- ROW 2 -->
<tr>
<th scope="row">Row 2</th>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><output class="rowsum output">0</output></td>
</tr>
<!-- ROW 3 -->
<tr>
<th scope="row">Row 3</th>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><output class="rowsum output">0</output></td>
</tr>
<!-- ROW 4 -->
<tr>
<th scope="row">Row 3</th>
<td><output class="colsum output">0</output></td>
<td><output class="colsum output">0</output></td>
<td><output class="colsum output">0</output></td>
<td><output class="colsum output">0</output></td>
<td><output class="rowsum output">0</output></td>
</tr>
</tbody>
</table>
</form>
<hr />
<!-- FORM 3 -->
<form>
<table>
<caption>
<strong>THIRD</strong>
</caption>
<tbody>
<!-- ROW 4 -->
<tr>
<th scope="row">Row 4</th>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><output class="rowsum output">0</output></td>
</tr>
<!-- ROW 5 -->
<tr>
<th scope="row">Row 5</th>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><output class="rowsum output">0</output></td>
</tr>
</tbody>
</table>
</form>
<hr />
<!-- FORM 4 -->
<form>
<table>
<caption>
<strong>FOURTH</strong>
</caption>
<tbody>
<!-- ROW 6 -->
<tr>
<th scope="row">Row 6</th>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><input type="number" value="0" /></td>
<td><output></output></td>
</tr>
<!-- ROW 7 -->
<tr>
<th scope="row">Row 7</th>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output></output></td>
</tr>
<!-- ROW 8 -->
<tr>
<th scope="row">Row 8</th>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output></output></td>
</tr>
<!-- ROW 9 -->
<tr>
<th scope="row">Row 9</th>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output></output></td>
</tr>
</tbody>
</table>
</form>
<hr />
<!-- FORM 5 -->
<form>
<table>
<caption>
<strong>FIFTH</strong>
</caption>
<tbody>
<!-- ROW 10 -->
<tr>
<th scope="row">Row 10</th>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="rowsum output">0</output></td>
</tr>
<!-- ROW 11 -->
<tr>
<th scope="row">Row 11</th>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="rowsum output">0</output></td>
</tr>
<!-- ROW 12 -->
<tr>
<th scope="row">Row 12</th>
<td><output class="editTable output output">0</output></td>
<td><output class="editTable output output">0</output></td>
<td><output class="editTable output output">0</output></td>
<td><output class="editTable output output">0</output></td>
<td><output class="rowsum output output">0</output></td>
</tr>
<!-- ROW 13 -->
<tr>
<th scope="row">Row 13</th>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="editTable output">0</output></td>
<td><output class="rowsum output">0</output></td>
</tr>
<!-- ROW 14 -->
<tr>
<th scope="row">Row 14</th>
<td><output class="colsum output">0</output></td>
<td><output class="colsum output">0</output></td>
<td><output class="colsum output">0</output></td>
<td><output class="colsum output">0</output></td>
<td><output class="rowsum output">0</output></td>
</tr>
</tbody>
</table>
</form>
</div>
<script src="/app.js"></script>
</body>
JS:
const forms = document.querySelectorAll("form");
forms.forEach(() => {
f0 = forms[0];
f1 = forms[1];
f2 = forms[2];
f3 = forms[3];
f4 = forms[4];
f0.onchange = editSheet;
f1.onchange = editSheet;
f2.onchange = editSheet;
f3.onchange = editSheet;
f4.onchange = editSheet;
});
function editSheet() {
const body = document.querySelectorAll("tbody");
const allrows = document.querySelectorAll("tr");
const input = document.querySelectorAll("input");
// unwanted for-loop. Gets the job done, but returns html-error in the console
for (i = 0; i < allrows.length; i++) {
rowTotal = 0;
for (ii = 0; ii < allrows[i].getElementsByTagName("input").length; ii++) {
rowTotal =
rowTotal + Number(allrows[i].getElementsByTagName("input")[ii].value);
}
allrows[i].querySelector(".rowsum").innerHTML = rowTotal;
}
/*
body.forEach(() => {
console.log(body[0]);
}); // gives every <tbody>
*/
/*
allrows.forEach((i, ix) => {
console.log(ix, i);
}); // gives every <tr>
*/
/*
input.forEach((i, ix) => {
console.log(ix, i.value);
}); // gives every value in the cells
*/
}
【问题讨论】:
标签: javascript foreach html-table calc