使用JavaScript基于HTML表的选定行,动态计算每列的总数
从图片中,我试图计算所选行A和c的每列总数。如果我将其从函数计算中解开,则代码有效。是否可以使其在包装功能中起作用。我希望能够选择一个行的变体来对每列的总和,而无需复制和粘贴代码并为每个列进行多次编辑。
function calculateCols(ID, calculate) {
var final = 0
var tbody = document.querySelector('tbody');
var howManyCols = tbody.rows[0].cells.length;
var totalRow = document.getElementById(ID);
for (var j = 1; j < howManyCols; j++) {
final = calculate;
const check = document.createElement('td');
check.innerText = final;
totalRow.appendChild(check);
}
function getRow(rowID) {
var result = 0;
try {
var check = document.getElementById(rowID)
var thisNumber = parseInt(check.cells[j].childNodes.item(0).data);
if (!isNaN(thisNumber))
result += thisNumber;
} finally {
return result;
}
}
}
calculateCols('total', getRow('a') + getRow('c'));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<table>
<tbody>
<tr>
<td></td>
<th scope="col">2013</th>
<th scope="col">2014</th>
<th scope="col">2015</th>
<th scope="col">2016</th>
</tr>
<tr id="a">
<th scope="row">a</th>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr id="b">
<th scope="row">b</th>
<td>5</td>
<td>6</td>
<td>4</td>
<td>5</td>
</tr>
<tr id="c">
<th scope="row">c</th>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr id="d">
<th scope="row">d</th>
<td>5</td>
<td>6</td>
<td>8</td>
<td>5</td>
</tr>
<tr id="total">
<th scope="row" id="Total">Total a + c</th>
</tr>
</tbody>
</table>
<script src="checkit.js"></script>
</body>
</html>
From the picture, I am trying to calculate the total of each column for the selected rows a and c. The code works if I unwrap it from the function calculateCols. Is it possible to make it work in the wrapped function. I want to be able to choose a variation of rows to sum for each column without copy and pasting the code and editing it multiple times for each one.
function calculateCols(ID, calculate) {
var final = 0
var tbody = document.querySelector('tbody');
var howManyCols = tbody.rows[0].cells.length;
var totalRow = document.getElementById(ID);
for (var j = 1; j < howManyCols; j++) {
final = calculate;
const check = document.createElement('td');
check.innerText = final;
totalRow.appendChild(check);
}
function getRow(rowID) {
var result = 0;
try {
var check = document.getElementById(rowID)
var thisNumber = parseInt(check.cells[j].childNodes.item(0).data);
if (!isNaN(thisNumber))
result += thisNumber;
} finally {
return result;
}
}
}
calculateCols('total', getRow('a') + getRow('c'));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<table>
<tbody>
<tr>
<td></td>
<th scope="col">2013</th>
<th scope="col">2014</th>
<th scope="col">2015</th>
<th scope="col">2016</th>
</tr>
<tr id="a">
<th scope="row">a</th>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr id="b">
<th scope="row">b</th>
<td>5</td>
<td>6</td>
<td>4</td>
<td>5</td>
</tr>
<tr id="c">
<th scope="row">c</th>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr id="d">
<th scope="row">d</th>
<td>5</td>
<td>6</td>
<td>8</td>
<td>5</td>
</tr>
<tr id="total">
<th scope="row" id="Total">Total a + c</th>
</tr>
</tbody>
</table>
<script src="checkit.js"></script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该做到:
我再次完全更改了答案:操作的中心部分现在是RPN计算器(函数
rpn()
),它贯穿了data-eqn
属性提供的指令字符串。所有这些都确定的结果 -&lt; tr&gt;
s。在计算开始之前,我在全局对象
d
中收集所有提供的表数据。所有tr
s都带有id
-Attribute将其td.td.td.textcontent
贡献到变为属性d [id] 。
d
最终是数组的对象。[[目前的结果限制为3位数字。可以再次删除
。tofixed(3)
可以轻松地关闭这。 ]]]]This should do it:
I changed my answer completely again: The centre piece of the operation is now an RPN calculator (function
rpn()
) that goes through instructions strings provided bydata-eqn
attributes in all so identified result-<tr>
s.Before the calculation can start I collect all provided table data in a global object
D
. Alltr
s with anid
-attribute contribute theirtd.textContent
to a vector which becomes propertyD[id]
.D
ends up being an object of arrays.[[ Currently the results are limited to 3 digits. This can easily be switched off by removing
.toFixed(3)
again. ]]您会改组范围(帮助: https://davidwalsh.name/for-and-for-and-gainst-let- )。
例如
check.cells [j]
始终返回howmanycols-1
。第一个错误(调试器也警告),
getrow
函数在calculatecols
函数中,但是呼叫(calculatecols('total'getrow('getrow('a a a a a a a a a) '),getrow('c'))
)不是我写了一个更简单的代码。
如果您只想获取
a
和c
行的金额,请设置列表行
['a'','c' ]
You shuffled the scopes (help: https://davidwalsh.name/for-and-against-let).
E.g.
check.cells[j]
always returnhowManyCols-1
.The first mistake (the debugger also warns), the
getRow
function is within thecalculateCols
function, but the call (calculateCols('total',getRow('a'),getRow('c'))
) is not.I have written a simpler code.
If you want to only get the
a
andc
rows' amount, set the listrows
['a','c']