I am trying to calculate the row and column total in an html table. However, I am trying to calculate the row total up to the second to last column. I can do it up to last column but not up to second to last. I also want to remove Total:0 from the first column. Can you please help me, below is my code:
<table id=sum_table width=300 border=1>
<tr class=titlerow>
<td></td>
<td>Apple</td>
<td>Orange</td>
<td>Watermelon</td>
<td>Total By Row</td>
<td>Strawberry</td>
</tr>
<tr>
<td> Row1</td>
<td class=rowAA>1</td>
<td class=rowAA>2</td>
<td class=rowBB>3</td>
<td class=totalRow></td>
<td class=rowBB>4</td>
</tr>
<tr>
<td> Row2</td>
<td class=rowAA>1</td>
<td class=rowAA>2</td>
<td class=rowBB>3</td>
<td class=totalRow></td>
<td class=rowBB>4</td>
</tr>
<tr>
<td>Row3</td>
<td class=rowAA>1</td>
<td class=rowAA>5</td>
<td class=rowBB>3</td>
<td class=totalRow></td>
<td class=rowBB>4</td>
</tr>
<tr class=totalColumn>
<td class=totalCol></td>
<td class=totalCol></td>
<td class=totalCol></td>
<td class=totalCol></td>
<td class=totalCol></td>
<td class=totalCol></td>
</tr>
</table>
JS:
$(#sum_table tr:not(:first,:last) td:nth-last-child(2)).text(function(){
var t = 0;
$(this).prevAll().each(function(){
t += parseInt( $(this).text(), 10 ) || 0;
});
return t;
});
$(#sum_table tr:last td).text(function(i){
var t = 0;
$(this).parent().prevAll().find(td:nth-child(+(++i)+)).each(function(){
t += parseInt( $(this).text(), 10 ) || 0;
});
return Total: + t;
});
I want the table to look like this format :
|Apples|Oranges|Watermelon|TotalRow|Strawberry|
Row1 |
Row2 |
Row3 |
Total|