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
| <!DOCTYPE html>
<html lang="fr">
<head>
<title>[Déplacement des lignes d'une <TABLE>]</title>
<meta name="Author" content="NoSmoking">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
body {
font-family : Verdana;
font-size : 1.0em;
}
h1 {
font-size : 1.5em;
color : #0000f0;
}
table {
border : 1px solid #c0c0c0;
border-collapse : collapse;
margin : 20px;
}
tr:hover {
background: #f0f0f0;
cursor : pointer;
}
td {
padding : 5px;
border : 1px solid #c0c0c0;
border-width : 0 1px 1px 0;
}
</style>
<script type="text/javascript">
function moveUpRow( obj){
// get TR parent
var oParent = obj.parentNode;
// get la position de TR
var ind = oParent.rowIndex;
// test faisabilite
if( ind > 0){
// get Parent Table
var oTable = oParent.parentNode;
// get toutes les lignes
var lstRow = oTable.rows;
// creation d'un copie de la TR
var oRow = oParent.cloneNode( true);
// remplace la ligne precedante
oRow = oTable.replaceChild( oRow, lstRow[ ind -1]);
// remplace la ligne courante avec TR recupere
oRow = oTable.replaceChild( oRow, lstRow[ ind]);
}
}
function moveDownRow( obj){
// get TR parent
var oParent = obj.parentNode;
// get la position de TR
var ind = oParent.rowIndex;
// get TABLE Parent
var oTable = oParent.parentNode;
// get toutes les lignes
var lstRow = oTable.rows;
// test faisabilite
if( ind < lstRow.length -1){
// creation d'un copie de la TR
var oRow = oParent.cloneNode( true);
// remplace la ligne suivante
oRow = oTable.replaceChild( oRow, lstRow[ ind +1]);
// remplace la ligne courante avec TR recupere
oRow = oTable.replaceChild( oRow, lstRow[ ind]);
}
}
</script>
</head>
<body>
<h1>Déplacement des lignes d'une <TABLE></h1>
<table>
<tr>
<td>Ligne 1, Colonne 1</td>
<td>Ligne 1, Colonne 2</td>
<td>Ligne 1, Colonne 3</td>
<td onclick="moveUpRow( this);">▲</td>
<td onclick="moveDownRow(this);">▼</td>
</tr>
<tr>
<td>Ligne 2, Colonne 1</td>
<td>Ligne 2, Colonne 2</td>
<td>Ligne 2, Colonne 3</td>
<td onclick="moveUpRow( this);">▲</td>
<td onclick="moveDownRow(this);">▼</td>
</tr>
<tr>
<td>Ligne 3, Colonne 1</td>
<td>Ligne 3, Colonne 2</td>
<td>Ligne 3, Colonne 3</td>
<td onclick="moveUpRow( this);">▲</td>
<td onclick="moveDownRow(this);">▼</td>
</tr>
<tr>
<td>Ligne 4, Colonne 1</td>
<td>Ligne 4, Colonne 2</td>
<td>Ligne 4, Colonne 3</td>
<td onclick="moveUpRow( this);">▲</td>
<td onclick="moveDownRow(this);">▼</td>
</tr>
</table>
</body>
</html> |
Partager