Jquery copy first column of table to each next column - html

I need for mobile view som manipulation of table. My table for desktop look like:
<table>
<thead>
<tr>
<th scope="row"> </th>
<th scope="col"> </th>
<th scope="col"><strong>Special Header</strong></th>
<th scope="col"> </th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><strong>First</strong></th>
<td><strong>Second</strong></td>
<td><strong>Third</strong></td>
<td><strong>fourth</strong></td>
</tr>
<tr>
<th scope="row"><strong>Text</strong></th>
<td>Text</td>
<td>Text</td>
<td>Text</td>
</tr>
</tbody>
</table>
For mobile devices I need to copy first colum to all other columns. Table should look like:
<table>
<thead>
<tr>
<th scope="row"> </th>
<th scope="col"> </th>
<th scope="row"> </th>
<th scope="col"><strong>Special Header</strong></th>
<th scope="row"> </th>
<th scope="col"> </th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><strong>First</strong></th>
<td><strong>Second</strong></td>
<th scope="row"><strong>First</strong></th>
<td><strong>Third</strong></td>
<th scope="row"><strong>First</strong></th>
<td><strong>fourth</strong></td>
</tr>
<tr>
<th scope="row"><strong>Text</strong></th>
<td>Text</td>
<th scope="row"><strong>Text</strong></th>
<td>Text</td>
<th scope="row"><strong>Text</strong></th>
<td>Text</td>
</tr>
</tbody>
</table>
I found some Jquery, but this code only append first column at the end:
$('#addcolumn').click(function() {
$('table tr').each(function() {
$(this).append($(this).find('th:first').clone());
});
});
Can you please help to make Jquery code working for my problem, thank!
Martin

You can use insertAfter this will insert element in the set of matched elements after the target .So, for tbody > td you can loop through tr and get first th cloned it then you can check if td is not last-child of tr if yes just add the cloned element after that td.Now , for thead you can simply use eq(0) to get first th and then use insertAfter to add the cloned after every th where scope="col".
Demo Code :
$('#addcolumn').click(function() {
///loop through tds
$('tbody td').each(function() {
//get th
var clone = $(this).closest("tr").find('th:first').clone()
//if td is not last
if (!$(this).is(':last-child')) {
$(clone).insertAfter(this); //insert the cloned th
}
});
//get the header first th
var headers = $("table").find("thead th:eq(0)").clone();
//insert cloned to th where scope=col and not last child
$(headers).insertAfter("thead th[scope=col]:not(:last-child)");
});
$('#normal').click(function() {
//remove th not first child from thead and tbody
$("thead th[scope=row]:not(:first-child)").remove();
$("tbody th[scope=row]:not(:first-child)").remove();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th scope="row"> </th>
<th scope="col"> </th>
<th scope="col"><strong>Special Header</strong></th>
<th scope="col"> </th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><strong>First</strong></th>
<td><strong>Second</strong></td>
<td><strong>Third</strong></td>
<td><strong>fourth</strong></td>
</tr>
<tr>
<th scope="row"><strong>Text</strong></th>
<td>Text</td>
<td>Text</td>
<td>Text</td>
</tr>
</tbody>
</table>
<button id="addcolumn">Add</button>
<button id="normal">Reset</button>

Related

React table optimization problem(HTML,CSS)

I am making a simple web page using React and I need a table on this page. The contents of the table are OK, but the table is not properly aligned. I do not see any problem in the code. How Can I Solve This?
import React from 'react'
const StuffList = () => {
return (
<div className='list-group'>
<table className="table table-hover table-danger"></table>
<thead>
<tr className="bg-primary">
<th scope="col">T.C</th>
<th scope="col">Name</th>
<th scope="col">Surname</th>
<th scope="col">City</th>
<th scope="col">Jobe</th>
<th scope="col">Salary</th>
<th scope="col">Hobbies</th>
<th scope="col">U.Graduate</th>
<th scope="col">Graduate</th>
<th scope="col">P.Graduate</th>
<th scope="col">Blood Group</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
<th scope="col">Details</th>
</tr>
</thead>
<tbody>
<tr>
<td>2826572</td>
<td>Mike</td>
<td>White</td>
<td>Kansas</td>
<td>Student</td>
<td>41</td>
<td>Parachute</td>
<td>no</td>
<td>no</td>
<td>no</td>
<td>ARH-</td>
<td><button className='btn btn-warning'>Update</button></td>
<td><button className='btn btn-danger'>Delete</button></td>
<td><button className='btn btn-info'>Details</button></td>
</tr>
</tbody>
</div>
)
}
export default StuffList
you can give the <tr style={{width:"100%",padding:"8px"}}> and for tds you can add
<td style={{padding:"8px}}>
if it didn't worked give the table and its parent div 100% width

Can't align the table head and the table body in html, using angular with in-memory-data

I can't seem to align the table head with the table body.
This is for my project making a web-type student data-storing database.
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">NIM</th>
<th scope="col">Nama</th>
<th scope="col">Jenis Kelamin</th>
<th scope="col">Alamat</th>
<th scope="col">Nama Bapak</th>
<th scope="col">Nama Ibu</th>
<th scope="col">Jurusan</th>
<th scope="col">Prodi</th>
<th scope="col">IPK Terakhir</th>
<th scope="col">Hapus</th>
</tr>
</thead>
<tbody *ngFor="let mhs of mhss">
<a routerLink="/detail/{{mhs.id}}">
<tr>
<td>{{mhs.id}}</td>
<td>{{mhs.nim}}</td>
<td>{{mhs.name}}</td>
<td>{{mhs.jk}}</td>
<td>{{mhs.alamat}}</td>
<td>{{mhs.nama_bapak}}</td>
<td>{{mhs.nama_ibu}}</td>
<td>{{mhs.jurusan}}</td>
<td>{{mhs.prodi}}</td>
<td>{{mhs.ipk}}</td>
<td><button class="delete" title="delete mhs"
(click)="delete(mhs)">HAPUS</button></td>
</tr>
</a>
</tbody>
</table>
My image:
Your code is generating a new <tbody> for every item in your array. Add the *ngfor structural directive to your table rows (<tr>).

Center <th> in bootstrap in double header table

Hello i have a table and i want to center a th between two other th.
I want to change the position of th Vieillesse de base to be displayed in the center of Provisionnel and Régularisation.
Here my HTML :
<div class="row mt-1">
<div class="col-12">
<table class="table table-striped table-adjust ">
<thead class="cordonnes-cabinet">
<tr>
<th scope="col">Date</th>
<th scope="col" style="text-align:center" colspan="2">Vieillesse de base</th>
<th scope="col" style="text-align:right">Complémentaire</th>
<th scope="col" style="text-align:right">Total</th>
</tr>
<tr>
<th scope="col">...</th>
<th scope="col" style="text-align:right">Provisionnel</th>
<th scope="col" style="text-align:right">Régularisation</th>
<th scope="col" style="text-align:right">...</th>
<th scope="col" style="text-align:right">...</th>
</tr>
</thead>
<tbody class="cordonnes-cabinet">
#for (int i = 0; i < echeancierGeneriqueAjustee.Count; i++)
{
<tr>
<th style="font-weight:100">#(echeancierGeneriqueAjustee[i]["Date"])</th>
<td style="text-align:right">#(echeancierGeneriqueAjustee[i]["MontantProv"])</td>
<td style="text-align:right">#(echeancierGeneriqueAjustee[i]["MontantRegul"])</td>
<td style="text-align:right">#(echeancierGeneriqueAjustee[i]["MontantComplement"])</td>
<td style="text-align:right">#(echeancierGeneriqueAjustee[i]["TotalEcheance"])</td>
</tr>
}
<tr>
<th style="font-size:14px">Total</th>
<td style="text-align:right">#(echeancierGeneriqueAjustee[0]["TotalProv"])</td>
<td style="text-align:right">#(echeancierGeneriqueAjustee[0]["TotalRegul"])</td>
<td style="text-align:right">#(echeancierGeneriqueAjustee[0]["TotalComplement"])</td>
<td style="text-align:right">#(echeancierGeneriqueAjustee[0]["Total"])</td>
</tr>
</tbody>
</table>
</div>
</div>
The key to do this is use html colspan in the th, to simulate equality of width with the second row. Then you only need to use .text-center.
In you particular case, use colspan="2" in the second th
JS FIDDLE DEMO
<table class="table">
<thead class="cordonnes-cabinet">
<tr>
<th>Date</th>
<th colspan="2" class="text-center">Vieillesse de base</th>
<th>Complémentaire</th>
<th>Total</th>
</tr>
<tr>
<th style="width:10%">...</th>
<th class="text-center">Provisionnel</th>
<th class="text-center">Régularisation</th>
<th>...</th>
<th>...</th>
</tr>
</thead>
<tbbody>
<tr>
<th>col1</th>
<td class="text-center">col2</td>
<td class="text-center">col3</td>
<td>col4</td>
<td>col5</td>
</tr>
</tbbody>
</table>
So the issue here is that your text is centered but the Column to the right is X wide and creates a optical illusion that your column is a lot wider.
I would set the column saying "Complémentaire" to text-align: left with class text-left then it will create the illuion you want.
Ive attached a picture where you can see that your code actually centers the text its just an optical illuion
This is how it would look with left aligned in the blue column:
And now its just a question on how to make it pretier with paddings on the td's and th's

Bootstrap table: change row color by data value

How do I change the color of a row in a bootstrap table, depending on the row's data value. (I want the text to be red if the date column is older that today).
I'm good with C++ and C#, but new to JS and JQuery
Here is the code:
<table data-toggle="table"
data-url="GenerateJSonEmailList.php"
data-sort-name="stargazers_count"
data-sort-order="desc">
<thead>
<tr>
<th data-field="idProspects"
data-sortable="true">
ID
</th>
<th data-field="Prenom"
data-sortable="true">
Prénom
</th>
<th data-field="Nom"
data-sortable="true">
Nom
</th>
<th data-field="EmailAddress"
data-sortable="true">
Adrese courriel
</th>
<th data-field="DateNextEmail"
data-sortable="true">
Date
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Assign class for your date field and loop it through jquery. Convert the date-field value into Date format then compare with the current date.
HTML
<table>
<thead>
<tr>
<th data-field="idProspects"
data-sortable="true">
ID
</th>
<th data-field="Prenom"
data-sortable="true">
Prénom
</th>
<th data-field="Nom"
data-sortable="true">
Nom
</th>
<th data-field="EmailAddress"
data-sortable="true">
Adrese courriel
</th>
<th data-field="DateNextEmail"
data-sortable="true">
Date
</th>
</tr>
</thead>
<tbody>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
<td>test</td>
<td class="datefield">05/11/2017</td>
</tr>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
<td>test</td>
<td class="datefield">05/23/2017</td>
</tr>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
<td>test</td>
<td class="datefield">05/18/2017</td>
</tr>
</tbody>
</table>
JQUERY
$(document).ready(function(){
var CurrentDate = new Date();
$('.datefield').each(function(){
var SelectedDate = new Date($(this).text());
if(CurrentDate > SelectedDate) {
$(this).css('color', 'red');
}
});
});
FIDDLE DEMO

How can automatically wrapped to a new line the text inside an HTML table cell?

I have the following problem in HTML.
I have this table that contains only a thead:
<table border="1" class="table_legenda" width="100%">
<thead>
<tr>
<th width="8.33%">Codice</th>
<th width="2%">Stato</th>
<th width="8.33%">Creazione<</th>
<th width="8.33%">Registrazione</th>
<th width="6.33%">Autore Cr</th>
<th width="6.33%">Autore Con</th>
<th width="6.33%">Autore Acq</th>
<th width="8.33%">Totale Imponibile</th>
<th width="24.66%">Fornitore</th>
<th width="4.33%">RM riserva</th>
<th width="8.33%">Errore</th>
<th width="8.33%">Azione</th>
</tr>
</thead>
</table>
Now, as you can see, every th cell have setted a percentual width and contains a textual value.
My problem is that if the textual value of a cell is longer than the available space of the cell it will be automatically hidden. I want that the text is automatically wrapped to a new line.
How can I do to obtain this behavior?
Tnx
You need to set the table to have a table-layout:fixed and the th elements to be overflow-wrap:break-word
.table_legenda {
table-layout: fixed;
}
.table_legenda th {
overflow-wrap: break-word;
}
<table border="1" class="table_legenda" width="100%">
<thead>
<tr>
<th width="8.33%">Codice</th>
<th width="2%">Stato</th>
<th width="8.33%">Creazione</th>
<th width="8.33%">Registrazione</th>
<th width="6.33%">Autore Cr</th>
<th width="6.33%">Autore Con</th>
<th width="6.33%">Autore Acq</th>
<th width="8.33%">Totale Imponibile</th>
<th width="24.66%">Fornitore</th>
<th width="4.33%">RM riserva</th>
<th width="8.33%">Errore</th>
<th width="8.33%">Azione</th>
</tr>
</thead>
</table>
demo at http://jsfiddle.net/ma4bc7zj/