One Function works on different tables - html

I have a table generated in a backend. My task is to wrap text in each header column and add it to each body column cells by using .html() and .prepend(). It works as expected (you will see text in green).
Problem: If there are 2 independent tables in a page, it will be mess up. Green text in the first table looks as expected, but it will take header text from the first table and add to body column cells of second table as an image below:
The 2 tables should be like the image below
How to fix this issue, please give me a hand. Thanks
$(document).ready(function() {
$("table thead tr th").each(function(i) {
let bodyCode = $("<span>", {
text: $(this).text()
});
$(`table tbody tr td:nth-child(${i+1})`).prepend(bodyCode);
});
});
table {
width: 100%;
border: 1px solid gray
}
span {
color: green
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
<table>
<thead>
<tr>
<th>Day</th>
<th>Earn</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<td>$100</td>
</tr>
<tr>
<td>Tuesday</td>
<td>$80</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>
</p>
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
<td>$200</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
<td>$180</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>

The main issue is that you need to loop through each table first.
So this code:
Gets each table element in the each.
Gets the th elements in that specific table using each
Gets the text you want from the th element and sets it to <span> tag
Then gets the current th element, finds its parent (table), and then finds each td element.
$(document).ready(function() {
// Loop through each table
$("table").each(function() {
// find all the tds in each table
let th = $(this).find('thead tr th');
// Loop through each th in the table
th.each(function(i) {
let bodyCode = $("<span>", {
text: $(this).text()
});
// find the td elements in this specific table.
$(this).parents('table').find(`tbody td:nth-child(${i+1})`).prepend(bodyCode);
});
});
});
table {
width: 100%;
border: 1px solid gray
}
span {
color: green
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Day</th>
<th>Earn</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<td>$100</td>
</tr>
<tr>
<td>Tuesday</td>
<td>$80</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>
<br>
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
<td>$200</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
<td>$180</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>
<p> added extra tables for proof of concept</p>
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
<td>$200</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
<td>$180</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>
<br>
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>

Related

How can I select the last row of first table in the html code?

<!DOCTYPE html>
<html>
<head>
<style>
table:first-child,
tr:nth-last-child(1) {
border: 1px solid black;
color: #ffff00;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
<tr>
<td>John</td>
<td>Wade</td>
</tr>
<tr>
<td>Liam</td>
<td>Mike</td>
</tr>
</table>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
<tr>
<td>John</td>
<td>Wade</td>
</tr>
<tr>
<td>Liam</td>
<td>Mike</td>
</tr>
</table>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
<tr>
<td>John</td>
<td>Wade</td>
</tr>
<tr>
<td>Liam</td>
<td>Mike</td>
</tr>
</table>
</body>
</html>
How can I select all the td elements in the last row of the first table on the webpage?
What happens is the whole table gets affected if I combine it with tr tag using the "," selector but if I code them separately then all the tables last rows get affected.
I just don't know how to combine table and tr correctly
Is this you wanted?
table:nth-child(1) tr:last-child td {
background-color: green;
}
Or:
table:first-child tr:last-child td {
background-color: green;
}
Use table:nth-child(1) for selecting only the first table itself. And then select the last row by tr:nth-last-child(1).
Try this:
table:nth-child(1) tr:nth-last-child(1) {
color: red;
}
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
<tr>
<td>John</td>
<td>Wade</td>
</tr>
<tr>
<td>Liam</td>
<td>Mike</td>
</tr>
</table>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
<tr>
<td>John</td>
<td>Wade</td>
</tr>
<tr>
<td>Liam</td>
<td>Mike</td>
</tr>
</table>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
<tr>
<td>John</td>
<td>Wade</td>
</tr>
<tr>
<td>Liam</td>
<td>Mike</td>
</tr>
</table>
/html/body/table[1]/tr[5]
This path represent your last row of first table.Similarly
/html/body/table[2]/tr[5] represents last row of second table.

Merge Column Into Rows In Html Table

Rowspan And Colspan For Building HTML Tables.
I want the first five column in a single row without any data as shown in editable fig. ,Can use rowspan or colspan.
Only The Last Two Column is as it is as shown in the editable fig. And There should be a row before that two column which is the combination of the five columns and that should be blank.
<div class="row">
<div class="col-12">
<table>
<thead>
<tr>
<th>Savings for holiday!</th>
<th>Date!</th>
<th>Year</th>
<th>Month</th>
<th>Time</th>
<th>Name</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr>
<td>$100</td>
<td>10/10/2020</td>
<td>2020</td>
<td>January</td>
<td>4:30pm</td>
<td>hrishi</td>
<td>male</td>
</tr>
</tbody>
<tbody>
<tr>
<td>$100</td>
<td>10/10/2020</td>
<td>2020</td>
<td>January</td>
<td>4:30pm</td>
<td>hrishi</td>
<td>male</td>
</tr>
</tbody>
</table>
</div>
</div>
See the code below.
<table>
<tr>
<th colspan="5" rowspan="3" width="300"></th>
<th>Name</th>
<th>Gender</th>
</tr>
<tr>
<td>hrishi</td>
<td>male</td>
</tr>
<tr>
<td>hrishi</td>
<td>male</td>
</tr>
</table>
I applied the bootstrap css class in css file and got my answer.
<table>
<tr>
<th colspan="5" rowspan="3" class="w-70"></th>
<th>Name</th>
<th>Gender</th>
</tr>
<tr>
<td>hrishi</td>
<td>male</td>
</tr>
<tr>
<td>hrishi</td>
<td>male</td>
</tr>
</table>
And add the width property in style.css file -
.w-70 {
width: 70%;
}

when can i use <tbody> <thead> <tfoot> insteadof the common tr th td?

When can I use <tbody> <thead> <tfoot> insteadof the common labels such as <tr> <th> <td>? IS the label such as <tbody> <thead> <tfoot> better than tr th td and why?
here you will find a documentation .
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
</table>

Highlight whole row regardless rowpsan - css

I have the code below:
tbody > tr:hover{
background-color: lightgrey;
cursor: pointer;
}
<table border="1">
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">January</td>
<td>Week 1</td>
<td >$150</td>
</tr>
<tr>
<td>Week 3</td>
<td>$100</td>
</tr>
<tr>
<td rowspan="2">February</td>
<td>Week 2</td>
<td >$50</td>
</tr>
<tr>
<td>Week 1</td>
<td>$80</td>
</tr>
</tbody>
</table>
How can I highlight the whole row when I hover to the row.
For example, when I hover to January, it should highlight the whole January row instead of highlight only the half one because of the rowspan attribute.
Move the styling to the td under the hovered tr. In addition, highlight the sibling row, when the 1st row is hovered. The only caveat is that if the 2nd tr is hovered, it won't highlight the 1st cell.
tr:hover > td {
background-color: lightgrey;
cursor: pointer;
}
tr:nth-child(2n + 1):hover + tr {
background-color: lightgrey;
}
<table border="1">
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">January</td>
<td>Week 1</td>
<td>$150</td>
</tr>
<tr>
<td>Week 3</td>
<td>$100</td>
</tr>
<tr>
<td rowspan="2">February</td>
<td>Week 2</td>
<td>$50</td>
</tr>
<tr>
<td>Week 1</td>
<td>$80</td>
</tr>
</tbody>
</table>
tr:hover td:not([rowspan]) {background: red;}
tr:hover td[rowspan]:hover ~ td {background: none;}
tr:hover td[rowspan]:hover {background: yellow;}
<table border="1">
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">January</td>
<td>Week 1</td>
<td >$150</td>
</tr>
<tr>
<td>Week 3</td>
<td>$100</td>
</tr>
<tr>
<td rowspan="2">February</td>
<td>Week 2</td>
<td >$50</td>
</tr>
<tr>
<td>Week 1</td>
<td>$80</td>
</tr>
</tbody>
</table>

Alignment of tables in HTML

How do I place 3 different tables - L1, L2, R1 in the specified below position:
L1 on Top Left - 2 rows
L2 immediately below L1 on the Left - 2 rows.
R1 on the Top Right - This has 10 rows
The issue is L2 is placed way below L1 as it is inline, how can I make it appear right below L1? Should float left / right or any other setting can help me achieve this?
https://jsfiddle.net/cyppyntp/4/
<table class="one" id="L1">
<tr>
<th>Month-1</th>
<th>Savings-1</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
<table class="one" id="L2">
<tr>
<th>Month-2</th>
<th>Savings-2</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$200</td>
</tr>
<tr>
<td>March</td>
<td>$100</td>
</tr>
<tr>
<td>April</td>
<td>$600</td>
</tr>
<tr>
<td>May</td>
<td>$100</td>
</tr>
<tr>
<td>June</td>
<td>$100</td>
</tr>
<tr>
<td>July</td>
<td>$100</td>
</tr>
<tr>
<td>August</td>
<td>$100</td>
</tr>
<tr>
<td>Sep</td>
<td>$100</td>
</tr>
<tr>
<td>Oct</td>
<td>$100</td>
</tr>
<tr>
<td>Nov</td>
<td>$100</td>
</tr>
</table>
<table class="two" id="R1">
<tr>
<th>Month-3</th>
<th>Savings-3</th>
</tr>
<tr>
<td>December</td>
<td>$400</td>
</tr>
</table>
.inlineTable {
display: inline-block;
}
table.one {
width:45%;
display:inline-block;
vertical-align:top;
}
table.two {
text-align:left;
}
See this fiddle
I've changed the order of your tables slightly. Also, I have added two new divs. See the HTML below
<div class="left">
<table class="one">
<tr>
<th>Month-1</th>
<th>Savings-1</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
<table class="two">
<tr>
<th>Month-3</th>
<th>Savings-3</th>
</tr>
<tr>
<td>December</td>
<td>$400</td>
</tr>
</table>
</div>
<div class="right">
<table class="one">
<tr>
<th>Month-2</th>
<th>Savings-2</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$200</td>
</tr>
<tr>
<td>March</td>
<td>$100</td>
</tr>
<tr>
<td>April</td>
<td>$600</td>
</tr>
<tr>
<td>May</td>
<td>$100</td>
</tr>
<tr>
<td>June</td>
<td>$100</td>
</tr>
<tr>
<td>July</td>
<td>$100</td>
</tr>
<tr>
<td>August</td>
<td>$100</td>
</tr>
<tr>
<td>Sep</td>
<td>$100</td>
</tr>
<tr>
<td>Oct</td>
<td>$100</td>
</tr>
<tr>
<td>Nov</td>
<td>$100</td>
</tr>
</table>
</div>
And, also added the below given CSS
.left,.right{
float:left;
width:50%;
}
I'm not a big fan of floats, but they work here without any change to your markup other than class values. You could accomplish the same without even that by using a :first-child selector.
.inlineTable {
display: inline-block;
}
table {
width: 45%;
vertical-align: top;
background: #ddd;
margin-bottom: 10px;
}
table.left {
float: left;
}
table.right {
float: right;
}
<table class="left">
<tr>
<th>Month-1</th>
<th>Savings-1</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
<table class="right">
<tr>
<th>Month-2</th>
<th>Savings-2</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$200</td>
</tr>
<tr>
<td>March</td>
<td>$100</td>
</tr>
<tr>
<td>April</td>
<td>$600</td>
</tr>
<tr>
<td>May</td>
<td>$100</td>
</tr>
<tr>
<td>June</td>
<td>$100</td>
</tr>
<tr>
<td>July</td>
<td>$100</td>
</tr>
<tr>
<td>August</td>
<td>$100</td>
</tr>
<tr>
<td>Sep</td>
<td>$100</td>
</tr>
<tr>
<td>Oct</td>
<td>$100</td>
</tr>
<tr>
<td>Nov</td>
<td>$100</td>
</tr>
</table>
<table class="left">
<tr>
<th>Month-3</th>
<th>Savings-3</th>
</tr>
<tr>
<td>December</td>
<td>$400</td>
</tr>
</table>
Demo