简体   繁体   English

如何使用 Javascript 对单个表列求和?

[英]How to sum a single table column using Javascript?

I have a generated table on my JSP that contains data for transactions: each individual transaction is a row, and there's a column for category, amount, type, and description.我的 JSP 上有一个生成的表,其中包含交易数据:每个单独的交易都是一行,并且有一列用于类别、金额、类型和描述。

<table class="table table-striped" id="res">
        <tr>
            <th>Category</th>
            <th>Amount</th>
            <th>Type</th>
            <th>Description</th>
        </tr>
        <c:forEach var="element" items="${sessionScope.pick}">
            <tr>
                <td><c:out value="${element.category}" /></td>
                <td class="countable">
                    <fmt:formatNumber type="currency" currencyCode="USD" 
                    value="${element.amount}"></fmt:formatNumber>
                </td>
                <td><c:out value="${element.entry_Type}" /></td>
                <td><c:out value="${element.description}" /></td>
            </tr>
        </c:forEach>
    </table>

So it comes out as所以它出来为

Category____Amount____Type____Description类别____数量____类型____描述

My table is populated using Struts: I select a department on another JSP, then press "display" to forward to the page that generates the table.我的表格是使用 Struts 填充的:我 select 一个部门在另一个 JSP 上,然后按“显示”转发到生成表格的页面。 Because of this, the table won't necessarily have a set number of rows.因此,该表不一定有一定数量的行。 What I'm trying to do is add up the Amount column from each transaction so I can display the total.我想要做的是将每笔交易的金额列相加,这样我就可以显示总数。 I tried to do this using Javascript but it hasn't worked for me:我尝试使用 Javascript 执行此操作,但它对我不起作用:

<script src="http://code.jquery.com/jquery-2.1.4.min.js">

    var cls = document.getElementById("res").getElementsByTagName("td");
    var sum = 0;
    for (var i = 0; i < cls.length; i++){
        if(tds[i].className == "countable"){
            sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
        }
    }
    document.getElementById("res").innerHTML.append("<tr><td> Total Balance </td><td>" + sum + "</td><td></td><td></td></tr>");
</script>

Can anyone see where I've messed up, or what a better option might be?谁能看到我在哪里搞砸了,或者有什么更好的选择? Also, is there a way to sum the columns and display the total without adding another row to the table?另外,有没有办法在不向表中添加另一行的情况下对列求和并显示总数? That would be ideal if possible.如果可能的话,那将是理想的。

Your variable tds should be named cls .您的变量tds应命名为cls Here is a working example.这是一个工作示例。

https://jsfiddle.net/34wf5gzs/ https://jsfiddle.net/34wf5gzs/

for (var i = 0; i < cls.length; i++){
    if(cls[i].className == "countable"){
        sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
    }
}

Roman C is right though . Roman C 是对的 You're better off summing this up using a JS framework or using a counter in JSP.最好使用 JS 框架或在 JSP 中使用计数器进行总结。 Custom scripts require some effort to maintain.自定义脚本需要一些努力来维护。

I have no knowledge about JSP but i assume that Roman C is right.我对 JSP 一无所知,但我认为 Roman C 是正确的。

But if you wan't to solve this with javascript here is a working example:但是如果你不想用 javascript 来解决这个问题,这里有一个工作示例:

var sum = 0;
var table = document.getElementById("res");
var ths = table.getElementsByTagName('th');
var tds = table.getElementsByClassName('countable');
for(var i=0;i<tds.length;i++){
    sum += isNaN(tds[i].innerText) ? 0 : parseInt(tds[i].innerText);
}

var row = table.insertRow(table.rows.length);
var cell = row.insertCell(0);
cell.setAttribute('colspan', ths.length);

var totalBalance  = document.createTextNode('Total Balance ' + sum);
cell.appendChild(totalBalance);

Link to a jsfiddle https://jsfiddle.net/4cetuvno/1/链接到 jsfiddle https://jsfiddle.net/4cetuvno/1/

I have table ABC with is having a column xyz.我有一个表 ABC 有一个 xyz 列。 Based on that we need to update input as "sum" as a total of xyz.基于此,我们需要将输入更新为“总和”作为 xyz 的总和。

function updateTotal(){
    var table = document.getElementById("ABC");
    var x = table.rows.length;
    var sumVal = 0;
    for (let i = 3; i < x-4; i++) {
        var value = table.rows[i].cells[3].getElementsByTagName('input')[0].value;
        if(value != null && ""!= value && !isNaN(value)){
            var valueToAdd = parseInt(value);
            sumVal = sumVal+valueToAdd;
        }
    }
    document.getElementById('sum').innerHTML="<td style='font-size: large;' id='sum'>"+sumVal+"</td>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM