codice:
<!DOCTYPE HTML>
<html lang="it">
<head>
<title>Esempio</title>
<meta charset="utf-8">
<style>
.tabella-ruoli {
table-layout: fixed;
border-collapse: collapse;
width: 800px;
max-width: 100%;
font: 14px sans-serif;
}
.tabella-ruoli .tabella-ruoli_nome {
font-style: italic;
}
.tabella-ruoli,
.tabella-ruoli th,
.tabella-ruoli td {
border: 1px solid Gray;
padding: 3px;
text-align: center;
word-break: break-all;
}
.tabella-ruoli > thead > tr:nth-child(1){
background-color: Orange;
}
.tabella-ruoli > thead > tr:nth-child(2){
background-color: Yellow;
}
.tabella-ruoli > tbody > tr[class] > *{
border-top-width: 2px;
}
.tabella-ruoli > tbody th{
background-color: WhiteSmoke;
}
.tabella-ruoli td:nth-of-type(1),
.tabella-ruoli td:nth-of-type(2){
text-transform: uppercase;
}
</style>
</head>
<body>
<table id="squadre" class="tabella-ruoli">
<thead>
<tr>
<th>Squadra</th>
<th class="tabella-ruoli_nome" colspan="3"></th>
</tr >
<tr>
<th>Ruolo</th>
<th>Nome</th>
<th>Squadra</th>
<th>Quotazione</th>
</tr>
</thead>
<tbody>
<tr class="tabella-ruoli_portieri"> <th>Portieri</th> <td colspan="3"></td></tr>
<tr class="tabella-ruoli_difensori"> <th>Difensori</th> <td colspan="3"></td></tr>
<tr class="tabella-ruoli_centrocampisti"><th>Centrocampisti</th><td colspan="3"></td></tr>
<tr class="tabella-ruoli_attaccanti"> <th>Attaccanti</th> <td colspan="3"></td></tr>
</tbody>
</table>
<script>
function aggiornaTabellaRuoli(sel,nom,arr){
var tabs = document.querySelectorAll(sel);
for (let t = 0; t < tabs.length; t++) {
let tab = tabs[t];
tab.querySelector('.tabella-ruoli_nome').innerText = nom;
let righeRuoli = {
p: tab.querySelector('.tabella-ruoli_portieri')
,d: tab.querySelector('.tabella-ruoli_difensori')
,c: tab.querySelector('.tabella-ruoli_centrocampisti')
,a: tab.querySelector('.tabella-ruoli_attaccanti')
}
for (let r = 0; r < arr.length; r++) {
let dat = arr[r];
let rigaRuolo = righeRuoli[dat[0].toLowerCase()];
if (!rigaRuolo) continue;
let strTd = '<td>'+dat[1]+'</td><td>'+dat[2]+'</td><td>'+dat[3]+'</td>';
if (rigaRuolo.querySelector('td:only-of-type')){
rigaRuolo.deleteCell(1);
rigaRuolo.insertAdjacentHTML('beforeend', strTd);
} else {
let tr = document.createElement('tr');
rigaRuolo.insertAdjacentHTML('afterend', '<tr>'+strTd+'</tr>');
rigaRuolo.querySelector('th').rowSpan++;
}
}
}
}
aggiornaTabellaRuoli(
'#squadre'
,'Alfa Team'
,[
['p','Handanovic','int',0]
,['d','Izzo','tor',0]
,['c','Chiesa','fio',0]
,['c','Milinkovic-Savic','laz',0]
,['d','Hateboer','ata',0]
,['a','Cristiano Ronaldo','juv',0]
,['p','Meret','nap',0]
,['d','Kalarov','rom',0]
,['a','Zapata D','ata',0]
,['a','Immobile','laz',0]
,['a','Belotti','tor',0]
,['d','N\'Koulou','tor',0]
,['c','Bernardeschi','juv',0]
,['d','Acerbi','laz',0]
,['c','Callejon','nap',0]
,['a','Higuain','juv',0]
,['p','Gollini','ata',0]
,['c','Under','rom',0]
,['a','Milik','nap',0]
,['c','Verdi','nap',0]
,['c','Kurtic','spa',0]
,['d','Alex Sandro','juv',0]
,['d','Mancini G','rom',0]
,['d','De Ligt','juv',0]
,['c','Bonaventura','mil',0]
]
);
</script>
</body>
</html>