IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Inverser abscisses et ordonnées d'un tableau HTML


Sujet :

JavaScript

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    44
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 44
    Points : 30
    Points
    30
    Par défaut Inverser abscisses et ordonnées d'un tableau HTML
    Bonjour,

    J'ai une <table> HTML qui a en axe X des dates et en axe Y des fruits.
    (exemple : )
    .................01/01.....01/02......01/03
    pommes......50.........40...........90
    poires..........15.........30...........25


    Quelqu'un connaîtrait une solution ou aurait un petit script sous la main qui permettrait d'inverser tout ça, de sorte que l'ont ait :

    ...........pommes....poires
    01/01.......50...........15
    01/02.......40...........30
    01/03.......90...........25




    Merci d'avance

  2. #2
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 665
    Points
    66 665
    Billets dans le blog
    1
    Par défaut
    Avec Jquery très facilement
    http://jsfiddle.net/CsgK9/103/

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    44
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 44
    Points : 30
    Points
    30
    Par défaut
    Merci SpaceFrog, t'es un chef!

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    44
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 44
    Points : 30
    Points
    30
    Par défaut
    Voici le script amélioré pour gérer les <th> :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    function() {
     
        var $this = $("#myTable");
        var newrows = [];
        $this.find("tr").each(function(){
            var i = 0;
            $(this).find("th").each(function(){
                i++;
                if(newrows[i] === undefined) { newrows[i] = $("<tr></tr>"); }
                newrows[i].append($(this));
            });
            $(this).find("td").each(function(){
                i++;
                if(newrows[i] === undefined) { newrows[i] = $("<tr></tr>"); }
                newrows[i].append($(this));
            });
        });
        $this.find("tr").remove();
        $.each(newrows, function(){
            $this.append(this);
        });
     
        $this.find("thead").html("<tr>"+$this.find("tr").html()+"</tr>");
        $this.find("tbody tr").first().remove();
      });
    jsFiddle ici : http://jsfiddle.net/vXS2q/12/

  5. #5
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant perpétuel
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Points : 22 933
    Points
    22 933
    Billets dans le blog
    125
    Par défaut
    @zzzer : Avec plusieurs tables (utiliser une classe au lieu d'un ID), et après plusieurs inversions, le résultat est surprenant !

    Voici ma solution et ma page de test, il suffit de copier-coller pour tester.

    La structure de la table inversée n'est pas conforme, mais cela a le mérite de fonctionner.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    <!DOCTYPE html>
    <html lang="fr">
    	<head>
    		<meta charset="utf-8">
    		<meta name="author" content="Daniel Hagnoul">
    		<title>Forum jQuery</title>
    		<link href='http://fonts.googleapis.com/css?family=Sofia|Ubuntu:400|Kreon'>
    		<link rel="stylesheet" href="http://danielhagnoul.developpez.com/styles/dvjhRemBase.css">
    		<link rel="stylesheet" href="http://danielhagnoul.developpez.com/lib/jPicker/css/jPicker.dvjh-1.1.6.min.css">
    		<style>
    			.hyphens { -moz-hyphens:auto; -webkit-hyphens:auto; -ms-hyphens:auto; hyphens:auto; }
    			article { display:table-cell; text-align:justify; /*border:0.1rem dotted grey;*/ }
    			.table1, .table2 { border-collapse:separate; border-spacing:0.6rem; }
    			.ligne { display:table-row; }
     
    			.table1 .ligne { height:10rem; }
    			.table1 article { width:160rem; }
     
    			.table1 .ligne article:nth-of-type(1) { vertical-align:top; text-align: center; }
     
    			.table2 .ligne { height:20rem; }
    			.table2 article { width:80rem; }
     
    			/* TEST */
    			th, td { padding:0.5rem; border:0.1rem dotted grey; }​
    		</style>
    	</head>
    	<body>
    		<h1>Forum jQuery</h1>
    		<h2>Inversion d'une table</h2>
    		<section class="conteneur">
    			<section class="table2">
    				<section class="ligne">
    					<article>
    <table class="inverse">
    	<thead>
    		<tr>
    		     <th>Lun</th>
    		     <th>Mar</th>
    		     <th>Mer</th>
    		     <th>Jeu</th>
    		     <th>Ven</th>
    		     <th>Sam</th>
    		     <th>Dim</th>		     
    		 </tr>
    	</thead>
    	<tbody>
    		<tr>
    			<td>0</td>
    			<td>1</td>
    			<td>2</td>
    			<td>3</td>
    			<td>4</td>
    			<td>5</td>
    			<td>6</td>
    		</tr>
    		<tr>
    			<td>1</td>
    			<td>2</td>
    			<td>3</td>
    			<td>4</td>
    			<td>5</td>
    			<td>6</td>
    			<td>7</td>
    		</tr>
    		<tr>
    			<td>2</td>
    			<td>3</td>
    			<td>4</td>
    			<td>5</td>
    			<td>6</td>
    			<td>7</td>
    			<td>8</td>
    		</tr>
    	</tbody>
    </table>
     
    <!-- Après inversion :
    <table class="inverse">
    	<thead>
    		<tr>
    			<th>Lun</th>
    			<td>0</td>
    			<td>1</td>
    			<td>2</td>
    		</tr>
    	</thead>
    	<tbody>
    		<tr>
    			<th>Mar</th>
    			<td>1</td>
    			<td>2</td>
    			<td>3</td>
    		</tr>
    		<tr>
    			<th>Mer</th>
    			<td>2</td>
    			<td>3</td>
    			<td>4</td>
    		</tr>
    		<tr>
    			<th>Jeu</th>
    			<td>3</td>
    			<td>4</td>
    			<td>5</td>
    		</tr>
    		<tr>
    			<th>Ven</th>
    			<td>4</td>
    			<td>5</td>
    			<td>6</td>
    		</tr>
    		<tr>
    			<th>Sam</th>
    			<td>5</td>
    			<td>6</td>
    			<td>7</td>
    		</tr>
    		<tr>
    			<th>Dim</th>
    			<td>6</td>
    			<td>7</td>
    			<td>8</td>
    		</tr>
    	</tbody>
    </table>
    -->
    					</article>
    					<article>
     
    <table class="inverse">
    	<thead>
    		<tr>
    		     <th>Prix en €/T</th>
    		     <th>01/01</th>
    		     <th>01/02</th>
    		     <th>01/03</th>
    		 </tr>
    	</thead>
    	<tbody>
    		<tr>
    			<th>Pommes</th>
    			<td>50</td>
    			<td>40</td>
    			<td>90</td>
    		</tr>
    		<tr>
    			<th>Poires</th>
    			<td>15</td>
    			<td>30</td>
    			<td>25</td>
    		</tr>
    	</tbody>
    </table>
     
    <!-- Après inversion :
    <table class="inverse">
    	<thead>
    		<tr>
    			<th></th>
    			<th>pommes</th>
    			<th>poires</th>
    		</tr>
    	</thead>
    	<tbody>
    		<tr>
    			<th>01/01</th>
    			<td>50</td>
    			<td>15</td>
    		</tr>
    		<tr>
    			<th>01/02</th>
    			<td>40</td>
    			<td>30</td>
    		</tr>
    		<tr>
    			<th>01/03</th>
    			<td>90</td>
    			<td>25</td>
    		</tr>
    	</tbody>
    </table>
    -->
    					</article>
    				</section>
    			</section>
    			<section class="table1">
    				<section class="ligne">
    					<article>
    <button id="btn">Inverser les tables</button>
    					</article>
    				</section>
    			</section>
    		</section>
    		<footer itemscope itemtype="http://danielhagnoul.developpez.com/">
    			<time datetime="2012-11-09T23:57:28.73+01:00" pubdate>2012-11-09T23:57:28.73+01:00</time>
    			<span itemprop="name">Daniel Hagnoul</span>
    			<a href="http://www.developpez.net/forums/u285162/danielhagnoul/" itemprop="url">@danielhagnoul</a>
    			<a href="http://danielhagnoul.developpez.com/" itemprop="url">Mon cahier d’exercices</a>
    			<a href="http://javascript.developpez.com/faq/jquery/" itemprop="url">FAQ</a>
    			<a href="http://javascript.developpez.com/cours/?page=frameworks#jquery" itemprop="url">Tutoriels</a>
    		</footer>
    		<script src="http://danielhagnoul.developpez.com/lib/raphael-min.js"></script>
    		<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    		<script src="http://danielhagnoul.developpez.com/lib/jPicker/jpicker-1.1.6.min.js"></script>
    		<script src="http://danielhagnoul.developpez.com/lib/dvjh/base.js"></script>
    		<script>
    			"use strict";
     
    			$( function(){
     
    				$( "#btn" ).click( function(){
     
    				    $("table.inverse").each( function( i, item ){
    				        var newrows = [];
     
    				        $( item )
    				        	.find("tr")
    				        		.each( function( j, jtem ){
    				        			var z = -1;
     
    						            $( jtem )
    						            	.find("th, td").each(function( k, ktem ){
    						            		z++;
     
    							                if ( newrows[z] === undefined ){
    							                	newrows[z] = $("<tr></tr>");
    							                }
     
    							                newrows[z].append( ktem );
    							            });
    						        })
    						        .remove();
     
    				        $.each( newrows, function( m, mtem){
    				        	if ( m == 0 ){
    				        		$( "thead", item ).append( mtem );
    				        	} else {
    				        		$( item ).append( mtem );
    				        	}
    				        });
    				    });
     
    				});
    			});
     
    			$( window ).load( function(){
     
    			});
    		</script>
    	</body>
    </html>

  6. #6
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    44
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 44
    Points : 30
    Points
    30

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Ré-ordonner un tableau HTML
    Par ludojojo dans le forum jQuery
    Réponses: 9
    Dernier message: 25/02/2014, 13h15
  2. Insertion javascript dans tableau HTML
    Par Rocca dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 08/08/2005, 12h28
  3. Tableau html décalage
    Par verticka dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 23/02/2005, 15h01
  4. [VB6] recuperer des valeurs ds un tableau html avec vb!!
    Par leo13 dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 11/12/2004, 13h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo