I have a table where I’m giving few amounts now I want to total of all amounts in table how can i do this?
My Code:-
JavaScript
x
18
18
1
var tr = "";
2
var totalAmount = "";
3
var footerTr = "";
4
for(var i=1; i<=31; i++){
5
tr += `<tr>
6
<td>${i}</td>
7
<td>${i*2}</td>
8
</tr>`
9
10
totalAmount += `${i*2}+`
11
}
12
footerTr = `<tr>
13
<th>Total</th>
14
<th>${totalAmount}</th>
15
</tr>`
16
17
$('#saving_calc tbody').append(tr);
18
$('#saving_calc tfoot').append(footerTr);
JavaScript
1
12
12
1
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
2
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js"></script>
3
<table class="table table-bordered"id="saving_calc" >
4
<thead>
5
<th>Date</th>
6
<th>Amount</th>
7
</thead>
8
<tbody>
9
</tbody>
10
<tfoot>
11
</tfoot>
12
</table>
Thanks for your efforts!
Advertisement
Answer
Like this:
JavaScript
1
23
23
1
var tr = "";
2
var totalAmount = "";
3
var total = 0; //this your total
4
var footerTr = "";
5
for(var i=1; i<=31; i++){
6
tr += `<tr>
7
<td>${i}</td>
8
<td>${i*2}</td>
9
</tr>`;
10
11
totalAmount += `${i*2}+`;
12
total += i*2; //this your total
13
}
14
totalAmount = totalAmount.substring(0, totalAmount.length - 1); // remove last plus
15
totalAmount += '='+total;
16
footerTr = `<tr>
17
<th>Total</th>
18
<th>${totalAmount}</th>
19
</tr>`;
20
21
$('#saving_calc tbody').append(tr);
22
$('#saving_calc tfoot').append(footerTr);
23
console.log(total);//this your total
JavaScript
1
12
12
1
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
2
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js"></script>
3
<table class="table table-bordered"id="saving_calc" >
4
<thead>
5
<th>Date</th>
6
<th>Amount</th>
7
</thead>
8
<tbody>
9
</tbody>
10
<tfoot>
11
</tfoot>
12
</table>