The sum of the columns in the table (input) - html

I need to make a table with dynamic column sums on a website. Unfortunately, I'm not very good at jQuery. Please help.
[enter image description here][1]
My table in HTML
[1]: https://i.stack.imgur.com/yV2Fo.png
table, td {
border: 1.25px solid black;
}
<table>
<thead>
<tr>
<td> NAME</td>
<td> Name 1 </td>
<td> Name 2 </td>
<td> Name 3 </td>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td><input type="number"</td>
<td><input type="number"</td>
<td><input type="number"</td>
</tr>
<tr>
<td>B</td>
<td><input type="number"</td>
<td><input type="number"</td>
<td><input type="number"</td>
</tr>
<tr>
<td>C</td>
<td><input type="number"</td>
<td><input type="number"</td>
<td><input type="number"</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>

I'm not sure how your table is set up. There are different ways you could do it, but I think this could be a solution:
I think something like this is what you are looking for?
Column
Total
A
150
B
100
C
50
Total
200
This is how you can accomplish this:
$(document).ready(() => {
let array = []; // Create an empty array
let sum = 0; // Set sum to 0 - This is only to set the data type to a number
/*
Use Jquery's :nth-child() selector to get target the 2nd cell in the table body
-- You can use :nth-child(3) for the 3rd cell etc...
Then use the $each method to perform a function.
The function will convert this cell's text (Using parseFloat() method) to a number and add (push()) it to our array
*/
$("table tbody td:nth-child(2)").each(function() {
array.push(parseFloat($(this).html()));
});
// Here we will loop over the array and add each value to the sum variable.
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
// Then append the last child (last table cell) in the table's footer with this sum value || Or if you gave the this cell an id, you can use that instead.
$('table tfoot td:last-child').html(sum);
});
/* This can be used over many columns by selecting the different nth-child()'s
*/
table, td {
border: 1.25px solid black;
}
td {
padding: 3px;
text-align: center;
}
td:last-child {
font-weight: bolder;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<!-- Table Heading -->
<thead>
<tr>
<td> Column </td>
<td> Total </td>
</tr>
</thead>
<!-- Table Body -->
<tbody>
<tr>
<td>A</td>
<td>150</td>
</tr>
<tr>
<td>B</td>
<td>100</td>
</tr>
<tr>
<td>C</td>
<td>50</td>
</tr>
</tbody>
<!-- Table Footer -->
<tfoot>
<tr>
<td>Total </td>
<!-- Add an id to this cell for the total or use my method in the js code-->
<td id="tableTotal"></td>
</tr>
</tfoot>
</table>
Please read the comments in the snippet, this will explain the process.

Related

Slightly complicated HTML table with merged rows

I have the following table:
Comm Layer
Implemented By
Application
Application
Transport
OS
Internet
OS
Link
OS
Link
Hardware
<table>
<thead>
<tr>
<th>Comm Layer</th>
<th>Implemented By</th>
</tr>
</thead>
<tbody>
<tr>
<td>Application</td>
<td>Application</td>
</tr>
<tr>
<td>Transport</td>
<td>OS</td>
</tr>
<tr>
<td>Internet</td>
<td>OS</td>
</tr>
<tr>
<td>Link</td>
<td>OS</td>
</tr>
<tr>
<td>Link</td>
<td>Hardware</td>
</tr>
</tbody>
</table>
I would like to merge the two cells that say "Link" and the three cells that say "OS". I tried using the rowspan attribute in several ways but to no avail. I was able to merge either the two "Link" cells or the three "OS" cells, but not both.
In short: you cannot have a <tr> where all cells participate in a rowspan="" because that creates a zero-height row (as there's no row-specific content). I feel this is a design flaw in HTML.
One workaround is to have a zero-width column that always has non-rowspan="" cells (which are propped up with , but hidden (using visibility: hidden;, not display: none;):
(My posted code comments out the removed cells with <!--<td>OS</td>--> for illustrative purposes, obviously you can remove those in your final version)
table {
border: 1px solid #999;
border-collapse: collapse;
}
th, td {
border: 1px solid #999;
}
tr > *:nth-child(1) { visibility: hidden; }
<table>
<thead>
<tr>
<th> </th>
<th>Comm Layer</th>
<th>Jurisdiction</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td>Application</td>
<td>Application</td>
</tr>
<tr>
<td> </td>
<td>Transport</td>
<td rowspan="3">OS</td>
</tr>
<tr>
<td> </td>
<td>Internet</td>
<!--<td>OS</td>-->
</tr>
<tr>
<td> </td>
<td rowspan="2">Link</td>
<!--<td>OS</td>-->
</tr>
<tr>
<td> </td>
<!--<td>Link</td>-->
<td>Hardware</td>
</tr>
</tbody>
</table>
There's probably improvements using more modern CSS techniques to enforce a minimum row height though - I've been using the technique since before I stopped using Dreamweaver in 2004.
<!-- Try this one -->
<table align="center" cellspacing="0" cellspadding=="0">
<thead>
<tr>
<th>Comm Layer</th>
<th>Jurisdiction</th>
</tr>
</thead>
<tbody>
<tr>
<td>Application</td>
<td>Application</td>
</tr>
<tr>
<td>Transport</td>
<td rowspan="2">OS</td>
</tr>
<tr>
<td>Internet</td>
</tr>
<tr>
<td rowspan="2">Link</td>
<td>OS</td>
</tr>
<tr>
<td>Hardware</td>
</tr>
</tbody>
</table>

Need to access the <td> with the help of the classname

I need to apply styles to the entire row based on some condition check on the class name.
My table structure looks like:
<table>
<tbody>
<tr>
<td><div><span class='level2'></span></div></td>
<td></td>
<td></td>
</tr>
<tr>
<td><div><span class='level90'></span></div></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
I need to apply the styles to the rest of the <td> in a row that contains class name as 'level2' in the first <td>.
How do I traverse through the <tr> till the <span> and apply the styles to the <td>'s of same row?
I did try something:
var els = document.getElementById("ratiosTable").getElementsByClassName("Level2");
console.log(els);
for (var i = 0; i < els.length; i++) {
els[i].style.fontWeight = "bold"
}
OPTION 1
You can iterate the rows and check that it has a child of the expected class.
var rows = document.getElementById("ratiosTable").getElementsByTagName("tr");
console.log(rows);
for(var i = 0; i < rows.length; i++){
if(rows[i].getElementsByClassName("level2").length > 0) {
rows[i].style.fontWeight = "bold"
}
}
<table id="ratiosTable">
<tbody>
<tr>
<td>
<div><span class='level2'>Bold</span></div>
</td>
<td>B</td>
<td>B</td>
</tr>
<tr>
<td>
<div><span class='level90'>Not Bold</span></div>
</td>
<td>N</td>
<td>N</td>
</tr>
</tbody>
</table>
OPTION 2
This does not work on IE but does on all other major browsers including Edge. For more information : https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
var els = document.getElementById("ratiosTable").getElementsByClassName("level2");
console.log(els);
for (var i = 0; i < els.length; i++) {
//NOTE closest
els[i].closest("tr").style.fontWeight = "bold"
}
<table id="ratiosTable">
<tbody>
<tr>
<td>
<div><span class='level2'>Bold</span></div>
</td>
<td>B</td>
<td>B</td>
</tr>
<tr>
<td>
<div><span class='level90'>Not Bold</span></div>
</td>
<td>N</td>
<td>N</td>
</tr>
</tbody>
</table>

Add alternating table row colour based on date?

I have a table in html and the first column in each row contains a date. I want to give some sort of separation based on date. So all rows with todays date would be dark grey, yesterdays row would be light grey and then two days ago would be a grey again and so on.
Is there an easy way to do this?
Thanks
To have alternating rows a different colour (with multiple rows having the same date), you'll have to use jQuery to iterate through all the table rows to check whether it should color that row or not.
Below is the jQuery, HTML and CSS for an example.
// iterate over each row
var tableDate = $("#MyTable tbody").parents('tr:first').find('td:first').text();
var shouldColor = true
$("#MyTable tbody tr").each(function(i) {
// find the first td in the row
var value = $(this).find("td:first").text();
// display the value in console
if (value == tableDate) {
if (shouldColor == true) {
$('#MyTable tbody tr:nth-child(' + (i + 1) + ')').addClass("alternate");
}
} else {
if (shouldColor == false) {
shouldColor = true
$('#MyTable tbody tr:nth-child(' + (i + 1) + ')').addClass("alternate");
} else {
shouldColor = false
}
}
tableDate = value
});
#MyTable {
width:100%;
border-collapse:collapse;
}
#MyTable td {
padding:7px; border:blue 1px solid;
}
#MyTable tr {
background: light-grey;
}
#MyTable .alternate {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table id="MyTable" >
<tbody>
<tr>
<td>4/4/2016</td> <td>Running</td>
</tr>
<tr>
<td>4/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>5/4/2016</td> <td>Running</td>
</tr>
<tr>
<td>6/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>6/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>6/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>6/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>7/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>7/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>8/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>9/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>10/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>11/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>11/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>12/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>12/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>12/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>13/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>13/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>13/4/2016</td> <td>Swimming</td>
</tr>
</tbody>
</table>

How do you use colspan and rowspan in HTML tables?

I don't know how to merge rows and columns inside HTML tables.
Can you please help me with making such a table in HTML?
If you're confused how table layouts work, they basically start at x=0, y=0 and work their way across. Let's explain with graphics, because they're so much fun!
When you start a table, you make a grid. Your first row and cell will be in the top left corner. Think of it like an array pointer, moving to the right with each incremented value of x, and moving down with each incremented value of y.
For your first row, you're only defining two cells. One spans 2 rows down and one spans 4 columns across. So when you reach the end of your first row, it looks something like this:
<table>
<tr>
<td rowspan="2"></td>
<td colspan="4"></td>
</tr>
</table>
Now that the row has ended, the "array pointer" jumps down to the next row. Since x position 0 is already taken up by a previous cell, x jumps to position 1 to start filling in cells. * See note about difference between rowspans.
This row has four cells in it which are all 1x1 blocks, filling in the same width of the row above it.
<table>
<tr>
<td rowspan="2"></td>
<td colspan="4"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
The next row is all 1x1 cells. But, for example, what if you added an extra cell? Well, it would just pop off the edge to the right.
<table>
<tr>
<td rowspan="2"></td>
<td colspan="4"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
* But what if we instead (rather than adding the extra cell) made all these cells have a rowspan of 2? The thing you need to consider here is that even though you're not going to be adding any more cells in the next row, the row still must exist (even though it's an empty row). If you did try to add new cells in the row immediately after, you'd notice that it would start adding them to the end of the bottom row.
<table>
<tr>
<td rowspan="2"></td>
<td colspan="4"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="2"></td>
<td rowspan="2"></td>
<td rowspan="2"></td>
<td rowspan="2"></td>
<td rowspan="2"></td>
</tr>
<tr>
<td></td>
</tr>
</table>
Enjoy the wonderful world of creating tables!
I'd suggest:
table {
empty-cells: show;
border: 1px solid #000;
}
table td,
table th {
min-width: 2em;
min-height: 2em;
border: 1px solid #000;
}
<table>
<thead>
<tr>
<th rowspan="2"></th>
<th colspan="4"> </th>
</tr>
<tr>
<th>I</th>
<th>II</th>
<th>III</th>
<th>IIII</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
References:
td element.
th element.
tbody element.
thead element.
table element.
If anyone is looking for a rowspan on both the left AND on the right,
here is how you can do it:
table {
border-collapse: collapse;
}
td {
padding: 20px;
border: 1px solid black;
text-align: center;
}
<table>
<tbody>
<tr>
<td rowspan="2">LEFT</td>
<td> 1 </td>
<td> 2 </td>
<td> 3 </td>
<td> 4 </td>
<td rowspan="2">RIGHT</td>
</tr>
<tr>
<td> 5 </td>
<td> 6 </td>
<td> 7 </td>
<td> 8 </td>
</tr>
<tr>
<td> - </td>
<td> - </td>
<td> - </td>
<td> - </td>
<td> - </td>
<td> - </td>
</tr>
</tbody>
</table>
Alternatively, if you want to add the LEFT and RIGHT to an existing rowset, you can achieve the same result by throwing them in with a collapsed colspan in between:
table {
border-collapse: collapse;
}
td {
padding: 20px;
border: 1px solid black;
text-align: center;
}
<table>
<tbody>
<tr>
<td rowspan="3">LEFT</td>
<td colspan="4" style="padding: 0; border-bottom: solid 1px transparent;"></td>
<td rowspan="3">RIGHT</td>
</tr>
<tr>
<td> 1 </td>
<td> 2 </td>
<td> 3 </td>
<td> 4 </td>
</tr>
<tr>
<td> 5 </td>
<td> 6 </td>
<td> 7 </td>
<td> 8 </td>
</tr>
<tr>
<td> - </td>
<td> - </td>
<td> - </td>
<td> - </td>
<td> - </td>
<td> - </td>
</tr>
</tbody>
</table>
Use rowspan if you want to extend cells down and colspan to extend across.
You can use rowspan="n" on a td element to make it span n rows, and colspan="m" on a td element to make it span m columns.
Looks like your first td needs a rowspan="2" and the next td needs a colspan="4".
The property you are looking for that first td is rowspan:
http://www.angelfire.com/fl5/html-tutorial/tables/tr_code.htm
<table>
<tr><td rowspan="2"></td><td colspan='4'></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
</table>
<style type="text/css">
table { border:2px black dotted; margin: auto; width: 100%; }
tr { border: 2px red dashed; }
td { border: 1px green solid; }
</style>
<table>
<tr>
<td rowspan="2">x</td>
<td colspan="4">y</td>
</tr>
<tr>
<td>I</td>
<td>II</td>
<td>III</td>
<td>IV</td>
</tr>
<tr>
<td>nothing</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
I have used ngIf for one of my similar logic. it is as follows:
<table>
<tr *ngFor="let object of objectData; let i= index;">
<td *ngIf="(i%(object.rowSpan))==0" [attr.rowspan]="object.rowSpan">{{object.value}}</td>
</tr>
</table>
here,
i'm getting rowspan value from my model object.
<body>
<table>
<tr><td colspan="2" rowspan="2">1</td><td colspan="4">2</td></tr>
<tr><td>3</td><td>3</td><td>3</td><td>3</td></tr>
<tr><td colspan="2">1</td><td>3</td><td>3</td><td>3</td><td>3</td></tr>
</table>
</body>
Colspan and Rowspan
A table is divided into rows and each row is divided into cells. In some situations we need the Table Cells span across (or merged) more than one column or row. In these situations we can use Colspan or Rowspan attributes.
Colspan
The colspan attribute defines the number of columns a cell should span (or merge) horizontally. That is, you want to merge two or more Cells in a row into a single Cell.
<td colspan=2 >
How to colspan ?
<html>
<body >
<table border=1 >
<tr>
<td colspan=2 >
Merged
</td>
</tr>
<tr>
<td>
Third Cell
</td>
<td>
Forth Cell
</td>
</tr>
</table>
</body>
</html>
Rowspan
The rowspan attribute specifies the number of rows a cell should span vertically. That is , you want to merge two or more Cells in the same column as a single Cell vertically.
<td rowspan=2 >
How to Rowspan ?
<html>
<body >
<table border=1 >
<tr>
<td>
First Cell
</td>
<td rowspan=2 >
Merged
</td>
</tr>
<tr>
<td valign=middle>
Third Cell
</td>
</tr>
</table>
</body>
</html>
It is similar to your table
<table border=1 width=50%>
<tr>
<td rowspan="2">x</td>
<td colspan="4">y</td>
</tr>
<tr>
<td bgcolor=#FFFF00 >I</td>
<td>II</td>
<td bgcolor=#FFFF00>III</td>
<td>IV</td>
</tr>
<tr>
<td>empty</td>
<td bgcolor=#FFFF00>1</td>
<td>2</td>
<td bgcolor=#FFFF00>3</td>
<td>4</td>
</tr>

A way to group table cells together in html?

So it is pretty straight forward. I need a way to group cells together. Like a <div> or a <span> but none of them worked. <tbody> seemed like a good solution but it only works for table rows. Help!
If you're looking for a way to merge 2 o more cells in a row into one single cell, along with other "regular" cells (as you would do in a google|excel spreadsheet) in a way similar to this:
then you can use the colspan attribute for td elements, indicating how many cells are you merging:
<tr>
<td colspan=2> Merged Cell occupying 2 columns </td>
</tr>
<tr>
<td> Regular cell </td>
<td> Another cell in same row </td>
</tr>
Additionally, you can use the td[colspan] selector in css (combined with any parent selector of your choice) to refer to these merged cells.
Here's a working example:
/* Style for cells with any colspan attribute */
td[colspan] {
text-align: center;
}
/* No extra space between cells */
table {
border-collapse: collapse;
}
th, td {
border: 1px solid gray;
margin: 0;
padding: 3px 10px;
text-align: right;
}
<table>
<tr>
<th>Day</th>
<th>Invoice</th>
<th>Total</th>
</tr>
<tr>
<!-- this cell will occupy 3 columns -->
<td colspan=3>January</td>
</tr>
<tr>
<td>2</td>
<td>0348</td>
<td>248.35</td>
</tr>
<tr>
<td>7</td>
<td>0349</td>
<td>126.14</td>
</tr>
<tr>
<td>18</td>
<td>0350</td>
<td>821.99</td>
</tr>
<tr>
<td colspan=3>February</td>
</tr>
<tr>
<td>27</td>
<td>0351</td>
<td>643.50</td>
</tr>
</table>
You can add the html col tag to group the columns td.
.col-group-1 {
background-color: yellow;
}
.col-group-2 {
background-color: silver;
}
<table>
<colgroup>
<col class="col-group-1">
<col span="2" class="col-group-2">
</colgroup>
<tr>
<th>Name</th>
<th>City</th>
<th>Phone</th>
</tr>
<tr>
<td>Mary</td>
<td>New york</td>
<td>987654321</td>
</tr>
<tr>
<td>Magdalena</td>
<td>Los Angeles</td>
<td>123456789</td>
</tr>
</table>
</body>
</html>
Please check out the html col tag
and how to use them with css styling