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 :

Table HTML manipulée par Javascript


Sujet :

JavaScript

  1. #1
    Nouveau membre du Club
    Inscrit en
    Avril 2007
    Messages
    56
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 56
    Points : 36
    Points
    36
    Par défaut Table HTML manipulée par Javascript
    Bonjour,
    Je cherche à grouper (rowSpan) certaines cellules de ma table HTML:
    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
     
        function function1() {
     
    		for(var i1=0;i1<5;i1++){
    		var target=document.getElementById('tab').rows[2];
    		try{
    		target.deleteCell(i1+1);}
    		catch(err){alert(i1+'_'+err.message);}
    		var target1=document.getElementById('tab').rows[1].cells;
    		target1[i1+1].rowSpan="2";
    		}
        }
    </script>
    <table border="1" id="tab" style="background-color:#f0f7f6">
    					<colgroup>
    						<col width="210"/>
    						<col width="300"/>
    						<col width="300"/>
    						<col width="300"/>
    						<col width="300"/>
    						<col width="300"/>
    					</colgroup>
    					<tbody>
    												<tr>
    								<!-- if checkbox is checked, clone school subjects to the whole table row  -->
    								<td ></td>
    								<td  style="color:#444;background-color:#e0e0e0;text-align:center">lundi</td>
    								<td  style="color:#444;background-color:#e0e0e0;text-align:center">mardi</td>
    								<td  style="color:#444;background-color:#e0e0e0;text-align:center">mercredi</td>
    								<td  style="color:#444;background-color:#e0e0e0;text-align:center">jeudi</td>
    								<td  style="color:#444;background-color:#e0e0e0;text-align:center">vendredi</td>
     
    							</tr>
    						<tr>
    							<td valign="top" id="td1" style="color:#444;background-color:#e0e0e0;height:15px;font-size:12px;font-weight:bold">8:00</td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    						</tr>
    						<tr>
    							<td valign="top" id="td2" style="color:#444;background-color:#e0e0e0;height:15px;font-size:12px;font-weight:bold">9:00</td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    						</tr>
    						<tr>
    							<td valign="top" id="td3" style="color:#444;background-color:#e0e0e0;height:15px;font-size:12px;font-weight:bold">9:30</td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    						</tr>
    						<tr>
    						<td valign="top" id="td4" style="color:#444;background-color:#e0e0e0;height:15px;font-size:12px;font-weight:bold">11:00</td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    							<td valign="top" ></td>
    						</tr>
    					</tbody>
    				</table>
    <input type="button" value="Remove Cell 24" onclick="function1();">
    Pour les 3 premières colonnes,pas de problème.
    Pour les colonnes 3 et 4, j'ai le message suivant:
    3(ou 4)_Index or size is negative or greater then the allowed amount.

    Que se passe t'il?
    D'avance merci.

  2. #2
    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 : 73
    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


    Le message d'erreur vous dit que vous essayez de manipuler des cellules inexistantes (débordement d'index).

    Voici un exemple, avec un code de "table" correct.

    Code HTML : 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
    <!DOCTYPE html>
    <html lang="fr" dir="ltr">
    <head>
      <meta http-equiv="cache-control" content="public, max-age=60">
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="author" content="Daniel Hagnoul">
      <title>Test</title>
      <style>
        
      </style>
      <script>
        'use strict';
        
        document.addEventListener( 'DOMContentLoaded', ev => {
          
        }, false );
        
        window.addEventListener( 'load', ev => {
          
          document.querySelector( "#btnRemove" ).addEventListener( 'click', ev => {
            const
              tbody = document.querySelector( "#tab > tbody" );
              
            let
              row,
              cells;
              
            for( let n = 0; n < tbody.rows.length; n++ ){
              row = tbody.rows[ n ];
              cells = row.cells;
              
              if ( ( n + 1  < tbody.rows.length ) && ( n % 2 === 0 ) ){
                
                // rowSpan 2 pour cell 1
                tbody.rows[ n + 1 ].deleteCell( 1 );
                cells[ 1 ].rowSpan = 2;
                
                // rowSpan 2 pour cell 3
                tbody.rows[ n + 1 ].deleteCell( 3 );
                cells[ 3 ].rowSpan = 2;
              }
            }
          }, false );
          
        }, false );
      </script>
    </head>
    <body>
      <main>
     
        <table border="1" id="tab" style="background-color:#f0f7f6">
          <colgroup>
            <col width="210"/>
            <col width="300"/>
            <col width="300"/>
            <col width="300"/>
            <col width="300"/>
            <col width="300"/>
          </colgroup>
          <thead>
            <tr>
              <!-- if checkbox is checked, clone school subjects to the whole table row  -->
              <th></th>
              <th  style="color:#444;background-color:#e0e0e0;text-align:center">lundi</th>
              <th  style="color:#444;background-color:#e0e0e0;text-align:center">mardi</th>
              <th  style="color:#444;background-color:#e0e0e0;text-align:center">mercredi</th>
              <th  style="color:#444;background-color:#e0e0e0;text-align:center">jeudi</th>
              <th  style="color:#444;background-color:#e0e0e0;text-align:center">vendredi</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td valign="top" id="td1" style="color:#444;background-color:#e0e0e0;height:15px;font-size:12px;font-weight:bold;text-align:center">8:00</td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
            </tr>
            <tr>
              <td valign="top" id="td2" style="color:#444;background-color:#e0e0e0;height:15px;font-size:12px;font-weight:bold;text-align:center">9:00</td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
            </tr>
            <tr>
              <td valign="top" id="td3" style="color:#444;background-color:#e0e0e0;height:15px;font-size:12px;font-weight:bold;text-align:center">9:30</td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
            </tr>
            <tr>
              <td valign="top" id="td4" style="color:#444;background-color:#e0e0e0;height:15px;font-size:12px;font-weight:bold;text-align:center">11:00</td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
              <td valign="top" ></td>
            </tr>
          </tbody>
        </table>
        <br>
        <button id="btnRemove">Remove Cell 24</button>      
     
      </main>
    </body>
    </html>

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

Discussions similaires

  1. [Article] Créer une table HTML éditable en JavaScript v2.0
    Par bigboomshakala dans le forum Général JavaScript
    Réponses: 27
    Dernier message: 16/05/2014, 16h32
  2. [Article] Créer une table HTML éditable en JavaScript v1.0
    Par bigboomshakala dans le forum Général JavaScript
    Réponses: 29
    Dernier message: 16/04/2012, 16h26
  3. Réponses: 4
    Dernier message: 22/01/2010, 10h24
  4. HTML généré par javascript : css ignoré par IE
    Par Espadrilles dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 19/06/2007, 09h18
  5. HTML modifié par Javascript
    Par Warz dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 15/03/2006, 21h53

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