简体   繁体   English

javascript 中的表总和特定列

[英]Table sum specific column in javascript

I'm having a trouble on how can I get the total amount column based on the status.我在如何根据状态获取总金额列时遇到问题。 Currently I only manage sum all of my amount column.目前我只管理我所有的金额列的总和。 The thing is I want to get the value of sum amount of paid and unpaid amount status, for example like the image below the Paid total should 15000 and the Unpaid should 10000 .问题是我想获得paidunpaid金额状态的总和值,例如像下图Paid total 应该15000Unpaid应该10000 It would be great if anybody could figure out where I am doing something wrong.如果有人能弄清楚我在哪里做错了,那就太好了。 thank you so much in advance非常感谢你

在此处输入图像描述

 var tds = document.getElementById('table').getElementsByTagName('td'); var sum = 0; for(var i = 0; i < tds.length; i ++) { if(tds[i].className == 'count-me') { sum += isNaN(tds[i].innerHTML)? 0: parseInt(tds[i].innerHTML); } } document.getElementById('table').innerHTML += '<tr><td>' + sum + '</td><td>total</td></tr>';
 <table id="table"> <tr> <th >Status</th> <th >AMOUNT</th> </tr> <tr> <td >Paid</td> <td class="count-me">5000</td> </tr> <tr> <td >Paid</td> <td class="count-me"> 5000</td> </tr> <tr> <td >Unpaid</td> <td class="count-me">5000</td> </tr> <tr> <td >Unpaid</td> <td class="count-me">5000</td> </tr> <tr> <td >Paid</td> <td class="count-me">5000</td> </tr> </table>

There are many ways.有很多方法。 This is one of them:这是其中之一:

 var tds = document.getElementById('table').getElementsByTagName('td'); var sum = 0; var sumPaid = 0; var sumUnpaid = 0; for(var i = 0; i < tds.length; i ++) { if(tds[i].className == 'count-me') { sumPaid += tds[i-1].innerHTML == 'Paid'? +tds[i].innerHTML: 0; sumUnpaid += tds[i-1].innerHTML == 'Unpaid'? +tds[i].innerHTML: 0; sum += isNaN(tds[i].innerHTML)? 0: parseInt(tds[i].innerHTML); } } document.getElementById('table').innerHTML += '<tr><td>' + sum + '</td><td>total</td></tr>'; document.getElementById('table').innerHTML += '<tr><td>' + sumPaid + '</td><td>total paid</td></tr>'; document.getElementById('table').innerHTML += '<tr><td>' + sumUnpaid + '</td><td>total unpaid</td></tr>';
 <table id="table"> <tr> <th >Status</th> <th >AMOUNT</th> </tr> <tr> <td >Paid</td> <td class="count-me">5000</td> </tr> <tr> <td >Paid</td> <td class="count-me"> 5000</td> </tr> <tr> <td >Unpaid</td> <td class="count-me">5000</td> </tr> <tr> <td >Unpaid</td> <td class="count-me">5000</td> </tr> <tr> <td >Paid</td> <td class="count-me">5000</td> </tr> </table>

I hope this is just like a homework or something.我希望这就像一个家庭作业或什么的。 In a real application you don't do things this way.在实际应用程序中,您不会以这种方式做事。 You make these kind of computations BEFORE rendering the page or using somekind of framework as a help.您在呈现页面或使用某种框架作为帮助之前进行此类计算。

I think a good advice would be for you to start learning to do things like they are done theses days with a framework like React or Angular, or even better with functional programming.我认为一个好的建议是让你开始学习使用 React 或 Angular 之类的框架,或者甚至更好地使用函数式编程,来学习这些天所做的事情。

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

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