简体   繁体   English

如何使用 javascript/jquery 在新表中对不同表和 append 的行求和

[英]How to sum rows of different table and append in a new table with javascript/jquery

I have six html tables each with there own theme.我有六个 html 表,每个表都有自己的主题。 Each table has two rows.每个表有两行。 Each row has to have a total sum.每行必须有一个总和。 The sum of each table has to be placed in a another table (the table_tot).每个表的总和必须放在另一个表(table_tot)中。 The code I discoverd does nearly the trick, but it sums all the row of all the tables at the end.我发现的代码几乎可以解决问题,但它在最后总结了所有表格的所有行。 In the code you can see that the second row in the table_tot is the sum of alle the rows of the two tables.在代码中,您可以看到 table_tot 中的第二行是两个表的所有行的总和。 I've tried to gave the td another class name, but that was not the solution.我试图给 td 另一个 class 名称,但这不是解决方案。 I hope someone has.我希望有人有。 My knowledge of javascript or jquery is very little.我对 javascript 或 jquery 的了解很少。

 var totals = [0, 0]; $(document).ready(function() { var $dataRows = $(".table_1 tr:not('.titlerow')"); $dataRows.each(function() { $(this).find('.count').each(function(i) { totals[i] += parseInt($(this).html()); }); $(".table_tot td.totalCol_1").each(function(i) { $(this).html(+totals[i]); }); }); var $dataRows = $(".table_2 tr:not('.titlerow')"); $dataRows.each(function() { $(this).find('.count').each(function(i) { totals[i] += parseInt($(this).html()); }); $(".table_tot td.totalCol_2").each(function(i) { $(this).html(+totals[i]); }); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="table_1"> <tr class="titlerow"> <th>Thema</th> <th>Moment 1</th> <th>Moment 2</th> </tr> <tbody> <tr> <td>Theme 1</td> <td class="count">4</td> <td class="count">5</td> </tr> <tr> <td>Theme 2</td> <td class="count">5</td> <td class="count">7</td> </tr> <tr> <td>Theme 3</td> <td class="count">8</td> <td class="count">6</td> </tr> <tr> <td>Theme 4</td> <td class="count">4</td> <td class="count">5</td> </tr> <tr> <td>Theme 5</td> <td class="count">4</td> <td class="count">7</td> </tr> <tr> <td>Theme 6</td> <td class="count">6</td> <td class="count">8</td> </tr> </tbody> </table> <table class="table_2"> <tr class="titlerow"> <th>Theme</th> <th>Moment 1</th> <th>Moment 2</th> </tr> <tbody> <tr> <td>Theme 1</td> <td class="count">4</td> <td class="count">6</td> </tr> <tr> <td>Theme 2</td> <td class="count">5</td> <td class="count">7</td> </tr> <tr> <td>Theme 3</td> <td class="count">8</td> <td class="count">6</td> </tr> <tr> <td>Theme 4</td> <td class="count">4</td> <td class="count">9</td> </tr> <tr> <td>Theme 5</td> <td class="count">4</td> <td class="count">7</td> </tr> <tr> <td>Theme 6</td> <td class="count">6</td> <td class="count">8</td> </tr> </tbody> </table> <table class="table_tot"> <tr> <th>Thema</th> <th>Moment 1</th> <th>Moment 2</th> </tr> <tr> <td>Theme 1</td> <td class="totalCol_1"></td> <td class="totalCol_1"></td> </tr> <tr> <td>Theme 2</td> <td class="totalCol_2"></td> <td class="totalCol_2"></td> </tr> </table>

The sums of the 2nd row of table_tot are the sums of all table rows because totals isn't [0,0] when the sums of table_2 are calculated, but [31,38] . table_tot的第二行的总和是所有表行的总和,因为在计算table_2的总和时, totals不是[0,0] ,而是[31,38] The easiest way to solve this is to have a 2nd totals variable:解决此问题的最简单方法是使用第二个totals变量:

 var totals = [0, 0]; var totals2 = [0, 0]; $(document).ready(function() { var $dataRows = $(".table_1 tr:not('.titlerow')"); $dataRows.each(function() { $(this).find('.count').each(function(i) { totals[i] += parseInt($(this).html()); }); $(".table_tot td.totalCol_1").each(function(i) { $(this).html(+totals[i]); }); }); var $dataRows = $(".table_2 tr:not('.titlerow')"); $dataRows.each(function() { $(this).find('.count').each(function(i) { totals2[i] += parseInt($(this).html()); }); $(".table_tot td.totalCol_2").each(function(i) { $(this).html(+totals2[i]); }); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="table_1"> <tr class="titlerow"> <th>Thema</th> <th>Moment 1</th> <th>Moment 2</th> </tr> <tbody> <tr> <td>Theme 1</td> <td class="count">4</td> <td class="count">5</td> </tr> <tr> <td>Theme 2</td> <td class="count">5</td> <td class="count">7</td> </tr> <tr> <td>Theme 3</td> <td class="count">8</td> <td class="count">6</td> </tr> <tr> <td>Theme 4</td> <td class="count">4</td> <td class="count">5</td> </tr> <tr> <td>Theme 5</td> <td class="count">4</td> <td class="count">7</td> </tr> <tr> <td>Theme 6</td> <td class="count">6</td> <td class="count">8</td> </tr> </tbody> </table> <table class="table_2"> <tr class="titlerow"> <th>Theme</th> <th>Moment 1</th> <th>Moment 2</th> </tr> <tbody> <tr> <td>Theme 1</td> <td class="count">4</td> <td class="count">6</td> </tr> <tr> <td>Theme 2</td> <td class="count">5</td> <td class="count">7</td> </tr> <tr> <td>Theme 3</td> <td class="count">8</td> <td class="count">6</td> </tr> <tr> <td>Theme 4</td> <td class="count">4</td> <td class="count">9</td> </tr> <tr> <td>Theme 5</td> <td class="count">4</td> <td class="count">7</td> </tr> <tr> <td>Theme 6</td> <td class="count">6</td> <td class="count">8</td> </tr> </tbody> </table> <table class="table_tot"> <tr> <th>Thema</th> <th>Moment 1</th> <th>Moment 2</th> </tr> <tr> <td>Theme 1</td> <td class="totalCol_1"></td> <td class="totalCol_1"></td> </tr> <tr> <td>Theme 2</td> <td class="totalCol_2"></td> <td class="totalCol_2"></td> </tr> </table>

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

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