1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Transposition de TABLE</title>
<meta name="Author" content="NoSmoking">
<style>
body {
margin:1em;
padding:0;
font:100%/150% Verdana,sans-serif;
}
table {
border-radius: 5px 5px 5px 5px;
border: 1px solid;
font-size: 15px;
}
td {
border-radius: 5px 5px 5px 5px;
border: 1px solid;
padding: 12px;
}
</style>
<script>
function transposeTable( ref){
// récup. objet ou id élément
var oTable = typeof( ref) === 'object' ? ref : document.getElementById( ref),
oTbody = oTable.tBodies[0],
nbLig = oTbody.rows.length,
nbCol = oTbody.rows[0].cells.length,
i, lig, col;
// création des TR
for( i=0; i <nbCol-nbLig; i++){
oTbody.appendChild( document.createElement('TR'));
}
// déplacement des TD via un appendChild
// on commence par la dernière colonne
col = nbCol;
while( col){
col--;
for( lig=0; lig <nbLig; lig++){
oTbody.rows[col].appendChild( oTbody.rows[lig].cells[col]);
}
}
// nettoyage des TR vide en partant de la fin de la table
lig = oTbody.rows.length -1;
while( !oTbody.rows[lig].cells.length){
oTbody.removeChild( oTbody.rows[lig]);
lig--;
}
}
function transposeCloneTable( id_table){
var oClone,
oTable = document.getElementById( id_table),
id_clone = oTable.id +'_clone';
// destruction si existant
oClone = document.getElementById( id_clone);
if( oClone){
oClone.parentNode.removeChild( oClone);
}
//création du clone
oClone = oTable.cloneNode( true);
// affectation de l'ID
oClone.id = id_clone;
// transpose le clone
transposeTable( oClone);
// ajout
oTable.parentNode.appendChild( oClone);
}
</script>
</head>
<body>
<h1>Transpose Table</h1>
<p>
<button onclick="transposeTable('datatable');">Transpose Table</button>
<button onclick="transposeCloneTable('datatable');">Transpose Clone Table</button>
</p>
<table border id="datatable" class="datatable_class">
<tbody>
<tr>
<td>td 01</td>
<td bgcolor="red"><input id="text1_2" value="td 02"></td>
</tr>
<tr>
<td>td 03</td>
<td bgcolor="green"><input id="text2_2" value="td 04"></td>
</tr>
<tr>
<td>td 05</td>
<td bgcolor="blue"><input id="text3_2" value="td 06"></td>
</tr>
<tr>
<td>td 07</td>
<td bgcolor="orange"><input id="text4_2" value="td 08"></td>
</tr>
<tr>
<td>td 09</td>
<td bgcolor="yellow"><input id="text5_2" value="td 10"></td>
</tr>
<tr>
<td bgcolor="grey">text 1</td>
<td bgcolor="grey">text 2</td>
</tr>
</tbody>
</table>
<p></p>
</body>
</html> |
Partager