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 :

suppression des lignes d'une table


Sujet :

JavaScript

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Février 2007
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 252
    Points : 85
    Points
    85
    Par défaut suppression des lignes d'une table
    Bonjour a tous,

    Finalemen je peux creer dynamiquement les lignes d'une table.

    mon code JS + php est le suivant:

    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
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <html>
    <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <script>
       function getXhr(){
                    var xhr = null; 
    				if(window.XMLHttpRequest) // Firefox and others
    				   xhr = new XMLHttpRequest(); 
    				else if(window.ActiveXObject){ // Internet Explorer 
    				   try {
    			                xhr = new ActiveXObject("Msxml2.XMLHTTP");
    			            } catch (e) {
    			                xhr = new ActiveXObject("Microsoft.XMLHTTP");
    			            }
    				}
    				else { // XMLHttpRequest not supported by your browser
    				   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
    				   xhr = false; 
    				} 
                                    return xhr
    			}
     
    			/**
    			*  method called when the user clicks on the button
    			*/
    			function go(){
    				var xhr = getXhr()
    				// We defined what we gonna do with the response
    				xhr.onreadystatechange = function(){
    					// We do somthing once the server's response is OK
    					if(xhr.readyState == 4 && xhr.status == 200){
    						//alert(xhr.responseText);
    						var body = document.getElementsByTagName("body")[0];
     
    					// Retrieve <table> ID and create a <tbody> element
     
    						var tbl = document.getElementById("table");
    						var tblBody = document.createElement("tbody");
    						var row = document.createElement("tr");
     
    					// Create  <td> elements and a text node, make the text
    					// node the contents of the <td>, and put the <td> at
    					// the end of the table row
    						var cell_1 = document.createElement("td");
    						var cell_2 = document.createElement("td");
    						var cell_3 = document.createElement("td");
    						var cell_4 = document.createElement("td");
     
    					// Create the first cell which is a text zone	
    						var cell1=document.createElement("input");
    						cell1.type="text";
    						cell1.name="fname";
    						cell1.size="20";
    						cell1.maxlength="50";
    						cell_1.appendChild(cell1);
     
    					// Create the second cell which is a text area	
    						var cell2=document.createElement("textarea");
    						cell2.name="fdescription";
    						cell2.rows="2";
    						cell2.cols="30";
    						cell_2.appendChild(cell2);
     
    						var cell3 = document.createElement("div");
    						cell3.innerHTML=xhr.responseText;
    						cell_3.appendChild(cell3);
     
     
    					// Create the fourth cell which is a href
    						var cell4 = document.createElement("a");
    						cell4.appendChild(document.createTextNode("[Delete]"));
    						cell4.setAttribute("href","delete.php");
    						cell_4.appendChild(cell4);
     
     
    					// add cells to the row
    						row.appendChild(cell_1);
    						row.appendChild(cell_2);
    						row.appendChild(cell_3);
    						row.appendChild(cell_4);
     
     
    					// add the row to the end of the table body
    						tblBody.appendChild(row);
     
    					// put the <tbody> in the <table>
    						tbl.appendChild(tblBody);
    					// appends <table> into <body>
    						body.appendChild(tbl);
    					// sets the border attribute of tbl to 2;
    						tbl.setAttribute("border", "1");
    				}
     
    					}
     
    				xhr.open("GET","fstatus.php",true);
    				xhr.send(null);
    			}
     
       </script>
     
    </head>
    <body >
    <h1> Create an Item </h1>
    <form method="post">
    	<table align="center" border = "2" cellspacing ="0" cellpadding="3" id="table"> 
    		<tr><td><b>Functionality Name:</b></td> <td><b>Description:</b></td> <td><b>Status:</b></td> <td><input type="button" Name= "Ajouter" Value="Ajouter" onclick="go()"></td></tr>
     
    	</table>	
    	<input type="submit" name ="submit" value="submit">
     
    </form>
    </form> 
    </body>
    </html>
    ce code execute par ajax ce simple code fstatus.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?php
                    // Retrieve all the statuses of a functionality
                                    echo '<select name="fstatus">';
                                    echo '<option value=1>Draft</option>';
                                    echo '<option value=2>Available</option>';
                                    echo '<option value=3>Private</option>';
                                    echo '<option value=4>Pilot</option>';
                                    echo '<option value=5>On Request</option>';
                                    echo '</select>';
                    
     ?>
    voila, ce code cree parfaitement mes lignes. Mais je voudrai savoir comment je peux faire pour supprimer une ligne? et aussi comment faire pour recuperer les donnees entrees par le user?

    Merci d'avance

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Février 2007
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 252
    Points : 85
    Points
    85
    Par défaut
    j'ai trouve le code suivant sur le net
    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
    <html>
    <head>
    <script type="text/javascript">
    function deleteRow(r){
    var i=r.parentNode.parentNode.rowIndex;
    document.getElementById('myTable').deleteRow(i);
    }
    </script>
    </head>
    <body>
     
    <table id="myTable" border="1">
    <tr>
      <td>Row 1</td>
      <td><input type="button" value="Delete" onclick="deleteRow(this)"></td>
    </tr>
    <tr>
      <td>Row 2</td>
      <td><input type="button" value="Delete" onclick="deleteRow(this)"></td>
    </tr>
    <tr>
      <td>Row 3</td><td> Col 2</td>
      <td><input type="button" value="Delete" onclick="deleteRow(this)"></td>
    </tr>
    </table>
     
    </body>
    </html>
    Et il marche parfaitement.
    mais quand je le teste:

    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
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <html>
    <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <script>
       function getXhr(){
                    var xhr = null; 
    				if(window.XMLHttpRequest) // Firefox and others
    				   xhr = new XMLHttpRequest(); 
    				else if(window.ActiveXObject){ // Internet Explorer 
    				   try {
    			                xhr = new ActiveXObject("Msxml2.XMLHTTP");
    			            } catch (e) {
    			                xhr = new ActiveXObject("Microsoft.XMLHTTP");
    			            }
    				}
    				else { // XMLHttpRequest not supported by your browser
    				   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
    				   xhr = false; 
    				} 
                                    return xhr
    			}
     
    			/**
    			*  method called when the user clicks on the button
    			*/
    			function go(){
    				var xhr = getXhr()
    				// We defined what we gonna do with the response
    				xhr.onreadystatechange = function(){
    					// We do somthing once the server's response is OK
    					if(xhr.readyState == 4 && xhr.status == 200){
    						//alert(xhr.responseText);
    						var body = document.getElementsByTagName("body")[0];
     
    					// Retrieve <table> ID and create a <tbody> element
     
    						var tbl = document.getElementById("table");
    						var tblBody = document.createElement("tbody");
    						var row = document.createElement("tr");
     
    					// Create  <td> elements and a text node, make the text
    					// node the contents of the <td>, and put the <td> at
    					// the end of the table row
    						var cell_1 = document.createElement("td");
    						var cell_2 = document.createElement("td");
    						var cell_3 = document.createElement("td");
    						var cell_4 = document.createElement("td");
     
    					// Create the first cell which is a text zone	
    						var cell1=document.createElement("input");
    						cell1.type="text";
    						cell1.name="fname";
    						cell1.size="20";
    						cell1.maxlength="50";
    						cell_1.appendChild(cell1);
     
    					// Create the second cell which is a text area	
    						var cell2=document.createElement("textarea");
    						cell2.name="fdescription";
    						cell2.rows="2";
    						cell2.cols="30";
    						cell_2.appendChild(cell2);
     
    						var cell3 = document.createElement("div");
    						cell3.innerHTML=xhr.responseText;
    						cell_3.appendChild(cell3);
     
     
    					// Create the fourth cell which is a href
    						/*var cell4 = document.createElement("a");
    						cell4.setAttribute("href","javascrit:DeleteRow(this);");
    						cell4.appendChild(document.createTextNode("[Delete]"));
    						cell_4.appendChild(cell4);*/
    						var cell4=document.createElement("input");
    						cell4.type="button";
    						cell4.value="Delete"
    						cell4.onclick=deleteRow(this);
    						cell_4.appendChild(cell4);
     
    					// add cells to the row
    						row.appendChild(cell_1);
    						row.appendChild(cell_2);
    						row.appendChild(cell_3);
    						row.appendChild(cell_4);
     
     
    					// add the row to the end of the table body
    						tblBody.appendChild(row);
     
    					// put the <tbody> in the <table>
    						tbl.appendChild(tblBody);
    					// appends <table> into <body>
    						body.appendChild(tbl);
    					// sets the border attribute of tbl to 2;
    						tbl.setAttribute("border", "1");
    				}
     
    					}
     
    				xhr.open("GET","fstatus.php",true);
    				xhr.send(null);
    			}
     
     
    	function deleteRow(r){
    		var i=r.parentNode.parentNode.rowIndex;
    		document.getElementById('table').deleteRow(i);
    	}
    </script>
     
     
    </head>
    <body >
    <h1> Create an Item </h1>
    <form method="post">
    	<table align="center" border = "2" cellspacing ="0" cellpadding="3" id="table"> 
    		<tr><td><b>Functionality Name:</b></td> <td><b>Description:</b></td> <td><b>Status:</b></td> <td><input type="button" Name= "Ajouter" Value="Ajouter" onclick="go()"></td></tr>
     
    	</table>	
    </form>
    </form> 
    </body>
    </html>
    j'obtiens
    Error: parentNode.parentNode is null or not an object
    Thanks

  3. #3
    Expert confirmé
    Avatar de RomainVALERI
    Homme Profil pro
    POOête
    Inscrit en
    Avril 2008
    Messages
    2 652
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : POOête

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 652
    Points : 4 164
    Points
    4 164
    Par défaut
    C'est le "this" passé en param à deleteRow qui ne va pas : dans le contexte de la fonction onreadystatechange il référence l'objet xhr et non pas l'élément à supprimer...

    essaie de modifier ton appel comme ça :
    Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
    cell4.onclick=deleteRow(row);
    ...row étant dans ton code la référence à la ligne du tableau à supprimer (si j'ai bien compris )

    >>> par contre et pour finir ^^ le code que tu as repris lançait la fonction de delete à partir de l'input dans le td, donc il fallait remonter de deux niveaux de parenté pour retrouver la ligne et la supprimer, d'où le "parentNode.parentNode"...
    Dans ton cas tu passes déjà une référence de la ligne, donc modifie ta fonction deleteRow en supprimant tes deux parentNode...

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Février 2007
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 252
    Points : 85
    Points
    85
    Par défaut
    Merci pour ta reponse et tes remarques
    on entete disparait quand je clique sur Ajouter.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    Line: 86
    Error: Not Implemented
    la ligne 86 est :
    cell4.onclick=deleteRow(row);
    je me demande si ceci est correcte.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    var cell4=document.createElement("input");
    cell4.type="button";
    cell4.value="Delete"
    cell4.onclick=deleteRow(row);
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    function deleteRow(r){
    	var i=r.rowIndex;
    	document.getElementById('table').deleteRow(i);
    }

  5. #5
    Expert éminent sénior

    Avatar de vermine
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    6 582
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2008
    Messages : 6 582
    Points : 79 912
    Points
    79 912
    Par défaut
    Je me demande si il ne faut pas faire ceci:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    cell4.onclick=deleteRow;
    Et avoir donc ici:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    function deleteRow()
    {
       var i= this.rowIndex;
       document.getElementById('table').deleteRow(i);
    }

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Février 2007
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 252
    Points : 85
    Points
    85
    Par défaut
    salut,
    Avec cette modification, quand je clique sur le bouton delete, la premiere fois, ca supprime l'entete de ma table. svp, pourquoi?

  7. #7
    Expert éminent sénior

    Avatar de vermine
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    6 582
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2008
    Messages : 6 582
    Points : 79 912
    Points
    79 912
    Par défaut
    Moi y en a pas savoir.
    Que contient i ? Et si vous faites i+1 (passez en float sinon il risque de perdre la tête à certains moments).
    Et à part supprimer la ligne d'en-tête, il supprime la ligne voulue aussi ?

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Février 2007
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 252
    Points : 85
    Points
    85
    Par défaut
    la premiere fois que je fais [delete], l'entete est supprimee. mais la ligne reste. quand je re-clique sur [delete], la ligne est supprimee.
    Qund j'ai essaye alert(i) reponse Undefined

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    function deleteRow(){
    	var i= this.rowIndex + 1;
    	//alert(i);
    	document.getElementById('table').deleteRow(i);
     
    }
    avec ce code, j'obtiens le meme resultat.

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Février 2007
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 252
    Points : 85
    Points
    85
    Par défaut
    je pense que je comprends le probleme.

    jái cree plusieurs lignes. et j'ai remarque que ma function deleteRow() supprime dábord l'entete ensuite supprime la ligne x- 1 quand on clique sur [Delete] pour la ligne x. pour x different 1

    Mais quand x = 1, la fonction deleteRow() supprime l'entete puis supprime effectivement la premiere ligne.

  10. #10
    Membre régulier
    Profil pro
    Inscrit en
    Février 2007
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 252
    Points : 85
    Points
    85
    Par défaut
    Je ne vois vraiment pas pourquoi j'ai ce probleme. dans l'exemple dont je me suis inspiree l'index change bien et Alert(i) renvoie un nombre. Mais dans mon cas alert(i) renvoie Undefined

  11. #11
    Expert éminent sénior

    Avatar de vermine
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    6 582
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2008
    Messages : 6 582
    Points : 79 912
    Points
    79 912
    Par défaut
    Et un alert de this.value, ça donne bien "Delete" ou ça ne donne rien ?

  12. #12
    Membre régulier
    Profil pro
    Inscrit en
    Février 2007
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 252
    Points : 85
    Points
    85
    Par défaut
    Citation Envoyé par vermine Voir le message
    Et un alert de this.value, ça donne bien "Delete" ou ça ne donne rien ?
    oui ca donne Delete

  13. #13
    Expert éminent sénior

    Avatar de vermine
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    6 582
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2008
    Messages : 6 582
    Points : 79 912
    Points
    79 912
    Par défaut
    Bien dans ce cas, nous sommes trop bas dans l'arborescence. Essayons de remettre les parentNode (le premier pour atteindre la cellule, le second pour atteindre la ligne):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    var i = this.parentNode.parentNode.rowIndex;

  14. #14
    Membre régulier
    Profil pro
    Inscrit en
    Février 2007
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 252
    Points : 85
    Points
    85
    Par défaut
    Citation Envoyé par vermine Voir le message
    Bien dans ce cas, nous sommes trop bas dans l'arborescence. Essayons de remettre les parentNode (le premier pour atteindre la cellule, le second pour atteindre la ligne):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    var i = this.parentNode.parentNode.rowIndex;
    Parfaitement raison. maintenant alert(i) donne les bonnes valeurs. et ca marche.
    Merci

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

Discussions similaires

  1. [AC-2007] Suppression des lignes d'une table
    Par solaar dans le forum VBA Access
    Réponses: 3
    Dernier message: 23/03/2012, 15h42
  2. Suppression automatique des lignes d'une table ODS
    Par pticouta dans le forum Approche théorique du décisionnel
    Réponses: 7
    Dernier message: 17/04/2011, 09h43
  3. suppression des ligne ds une table
    Par roger.pouamoun dans le forum Oracle
    Réponses: 6
    Dernier message: 28/07/2006, 12h00
  4. Ajout/Suppression dynamique des lignes dans une table
    Par codexomega dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 13/08/2005, 18h50
  5. Réponses: 4
    Dernier message: 31/05/2004, 12h26

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