I have a table with five columns that has the following structure:
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.css" rel="stylesheet"/>
<table class="table table-bordered">
<tbody>
<tr>
<td>Timestamp</td>
<td>Cash & Carry</td>
<td>Shop Name</td>
<td>Amount</td>
<td>Comments</td>
</tr>
<tr>
<td>10/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>J's Pizza Stop </td>
<td>72.75£</td>
<td>PAID 2018-01-11 16:53 Full Amount: 75 Discount of 3% Discount Total: 2.25</td>
</tr>
<tr>
<td>13/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Mexican House</td>
<td>93.42£</td>
<td>PAID 2018-01-18 10:08 Full Amount: 96.31 Discount of 3% Discount Total: 2.8893</td>
</tr>
<tr>
<td>14/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Mexican House</td>
<td>67.08£</td>
<td>PAID 2018-01-18 10:05 Full Amount: 69.15 Discount of 3% Discount Total: 2.0745</td>
</tr>
<tr>
<td>18/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Mexican House</td>
<td>94.00£</td>
<td>PAID 2018-01-25 10:34 Full Amount: 96.91 Discount of 3% Discount Total: 2.9073</td>
</tr>
<tr>
<td>19/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Mexican House</td>
<td>48.50£</td>
<td>PAID 2018-01-25 10:34 Full Amount: 50 Discount of 3% Discount Total: 1.5</td>
</tr>
<tr>
<td>20/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Mexican House</td>
<td>34.85£</td>
<td>PAID 2018-01-25 10:33 Full Amount: 35.93 Discount of 3% Discount Total: 1.0779</td>
</tr>
<tr>
<td>25/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Eastham Express</td>
<td>212.97£</td>
<td>PAID 2018-02-01 10:51 Full Amount: 219.56 Discount of 3% Discount Total: 6.5868</td>
</tr>
<tr>
<td>03/02/2018</td>
<td>New Delhi Cash & Carry</td>
<td>J's Pizza Stop </td>
<td>14.55£</td>
<td>Full Amount: 15 Discount of 3% Discount Total: 0.45</td>
</tr>
</tbody>
</table>
I want to sum the values of the Amount column and display a total amount in a row under the amount column. My question is, is there a way in which that can be done so that it can look like the picture below?
So far the answers I’ve seen only added a table footer that displays under every column. Is it at all possible to add just 1 row?
7
Answers
Try keeping the info in a JSON file, then generating the table with Javascript’s
createElement()
andappendChild()
methods. With all the information in the JSON file, you can easily sum the amounts and add another node at the end of each table row with special formatting with the:last-child
selector.You can append a total row to the
table
As said in the comments, you can simply append a nearly empty
<tr>
to your table :Try the following:
Yes, there is an DOM attribute called
colspan
, it is used to control how many columns does a<td>
cross over.You can add a row and some CSS to do the trick:
You can append footer, and just style borders with css
Use this selector
'table tbody tr:gt(0) td:nth-child(4)'
Look at this code snippet
See? the total amount was calculated.
Resources
:nth-child() Selector
:gt() Selector