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 d'une ligne dans un tableau


Sujet :

JavaScript

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    829
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 829
    Par défaut Suppression d'une ligne dans un tableau
    bonsoir à tous,


    j'ai une page web ou il y a du javascript pour la création d'un taleau html. il fonctionne mais je voudrait arrivé a supprimer la dernier ligne de mon tableau.

    En fait je remplis les case input, je clique sur ajouter et cela créer un tableau html.

    Mais je voudrais qu'au depart il supprime la derniere ligne ( cette ligne est créé plus bas pour faire la somme du colonne.)

    J'ai essayé ceci:

    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
     
     
    ar EMB =
     
        {
     
            nbEmballage : 0,
     
            idEmballage : 0,
     
            ajouter : function ()
     
            {
     
    		if (EMB.nbEmballage == 0)
                {
                }
    		else
    		{
    		var tbody = document.getElementById('liste-emballage');
    		row = document.getElementById("ajout").rows.length;
    		alert(row);
    		document.getElementById('ajout').deleteRow(row);
    		}
    		            var e = document.getElementById('emballage');
                var qe = document.getElementById('qte_emb');
    			var pu = document.getElementById('pu_emb');
    			var total = document.getElementById('pu_emb').value.replace(/(,)/gi, ".") * document.getElementById('qte_emb').value.replace(/(,)/gi, ".");
                var tbody = document.getElementById('liste-emballage');
                if (EMB.nbEmballage == 0) { tbody.deleteRow(0); }
                var tr = tbody.insertRow(tbody.rows.length);
                var td1 = tr.insertCell(0);
                var td2 = tr.insertCell(1);
                var td3 = tr.insertCell(2);
    			var td4 = tr.insertCell(3);
    			var td5 = tr.insertCell(4);
     
                // Insertion produit
     
                var emb = document.createTextNode(e.options[e.selectedIndex].text);
                var embInput = document.createElement('input');
                embInput.setAttribute('type', 'hidden');
                embInput.setAttribute('name', 'emballage[' + EMB.idEmballage + '][0]');
                embInput.setAttribute('value', e.options[e.selectedIndex].value);
                td1.appendChild(emb);
                td1.appendChild(embInput);
     
                // Insertion quantité
     
                var embqtte = document.createTextNode(qe.value);
                var embqtteInput = document.createElement('input');
                embqtteInput.setAttribute('type', 'hidden');
                embqtteInput.setAttribute('name', 'qte[' + EMB.idEmballage + '][1]');
                embqtteInput.setAttribute('value', qe.value.replace(/(,)/gi, "."));
                td2.appendChild(embqtte);
                td2.appendChild(embqtteInput);
     
                            // Insertion prix
     
                var embprix = document.createTextNode(pu.value);
                var embprixInput = document.createElement('input');
                embprixInput.setAttribute('type', 'hidden');
                embprixInput.setAttribute('name', 'pu[' + EMB.idEmballage + '][2]');
                embprixInput.setAttribute('value', pu.value.replace(/(,)/gi, "."));
                td3.appendChild(embprix);
                td3.appendChild(embprixInput);
     
    			// Bouton supprimer
     
                var btnSupprimer = document.createElement('input');
                btnSupprimer.setAttribute('type', 'button');
                btnSupprimer.setAttribute('value', 'Supprimer');
                btnSupprimer.onclick = EMB.supprimer;
                td4.appendChild(btnSupprimer);
     
    			// Total par produit:
    			//alert ('test'+total);
    			//var embtotal = document.getElementById('pu_emb').value.replace(/(,)/gi, ".") * document.getElementById('qte_emb').value.replace(/(,)/gi, ".");
                var embtotal = document.createTextNode(total);
    			var embtotalInput = document.createElement('input');
                embtotalInput.setAttribute('type', 'hidden');
                embtotalInput.setAttribute('name', 'total[' + EMB.idEmballage + '][3]');
                embtotalInput.setAttribute('value', total);
     
    			td5.appendChild(embtotal);
                td5.appendChild(embtotalInput);
     
     
    			//Calcul Complet total
    			var tr =  tbody.insertRow(tbody.rows.length);
    			var td1 = tr.insertCell(0);
    			var td2 = tr.insertCell(1);
    			var total = document.createTextNode('test');
    			td1.setAttribute('colSpan', 4);
    			td1.appendChild(total);
    			//var montanttotal = 0;
    			//var nbLignes3 = document.getElementById("ajout").rows.length - 2;
    			//for (a=1;a<=nbLignes3;a++)
    			//{
    			//montanttotal = montanttotal + parseFloat(document.getElementById('total[' + a + '][3]').value);
               // }
    			//alert ('montanttotal'+motanttotal);
    			//var total = document.createTextNode(totaltotal);
    			//var totalInput = document.createElement('input');
               // totalInput.setAttribute('type', 'hidden');
               // totalInput.setAttribute('name', 'total');
               // totalInput.setAttribute('value', total);
     
    			//td2.appendChild(total);
                //td2.appendChild(totalInput);	
     
     
     
    			// Inc nbProduits et id
                EMB.nbEmballage++;
                EMB.idEmballage++;
     
            },
     
            supprimer : function ()
     
            {
     
                var tbody = document.getElementById('liste-emballage');
                var row = this.parentNode.parentNode;
                var index = 0;
                while (row = row.previousSibling)
                {
                    if (row.tagName) { index++; }
                }
     
                tbody.deleteRow(index);
                EMB.nbEmballage--;
                if (EMB.nbEmballage == 0)
                {
                    var tr = tbody.insertRow(0);
                    var td = tr.insertCell(0);
                    td.setAttribute('colSpan', 5);
                    td.appendChild(document.createTextNode('Aucun emballage'));
                }
            },
     
            check : function ()
            {
                if (EMB.nbEmballage < 1) alert('Il faut au moins 1 emballage');
                return EMB.nbEmballage > 0;
            }
        };
    uncaught exception: Index or size is negative or greater than the allowed amount (NS_ERROR_DOM_INDEX_SIZE_ERR)
    [Break on this error] document.getElementById('ajout').deleteRow(row);


    Ou Est je commis une erreur?
    le code erreur est celui ci (c'est celui qui doit faire la suppression de la dernière ligne):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    var tbody = document.getElementById('liste-emballage');
          row = document.getElementById("ajout").rows.length;
          alert(row);
          document.getElementById('ajout').deleteRow(row);
          }
    Merci

    guigui69

  2. #2
    Rédacteur

    Avatar de Bovino
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Billets dans le blog
    20
    Par défaut
    C'est l'indice qui n'est pas bon... length commence à compter à 1 mais les indices commencent à 0 !
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    document.getElementById('ajout').deleteRow(row-1);
    Pas de question technique par MP !
    Tout le monde peut participer à developpez.com, vous avez une idée, contactez-moi !
    Mes formations video2brain : La formation complète sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
    Mon livre sur jQuery
    Module Firefox / Chrome d'intégration de JSFiddle et CodePen sur le forum

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    829
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 829
    Par défaut
    Merci pour ta réponse.

    J'ai encore un petit souci , mais que j'arrive pas a comprendre.

    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
     
    // Total par produit:
    			//alert ('test'+total);
    			//var embtotal = document.getElementById('pu_emb').value.replace(/(,)/gi, ".") * document.getElementById('qte_emb').value.replace(/(,)/gi, ".");
                var embtotal = document.createTextNode(total);
    			var embtotalInput = document.createElement('input');
                embtotalInput.setAttribute('type', 'hidden');
                embtotalInput.setAttribute('name', 'total[' + EMB.idEmballage + '][3]');
    			embtotalInput.setAttribute('id', 'total[' + EMB.idEmballage + '][3]');
                embtotalInput.setAttribute('value', total);
     
    			td5.appendChild(embtotal);
                td5.appendChild(embtotalInput);
    			alert('nom:'+embtotalInput.name);
    			alert('total:'+embtotalInput.value);
    			// Inc nbProduits et id
                EMB.nbEmballage++;
                EMB.idEmballage++;
     
    			//Calcul Complet total
    			var tr =  tbody.insertRow(tbody.rows.length);
    			var td1 = tr.insertCell(0);
    			var td2 = tr.insertCell(1);
    			var total = document.createTextNode('test');
    			td1.setAttribute('colSpan', 4);
    			td1.appendChild(total);
    			//var document.getElementById('total[0][3]').value = 0;
    			var montanttotal = 0;
    			var nbLignes3 = document.getElementById("ajout").rows.length;
    			alert ('nbre:'+nbLignes3);
    			for (a=0;a<=nbLignes3;a++)
    			{
    			alert ('nom:'+embtotalInput.name+'valeur==>'+document.getElementById('total['+ a +'][3]').value);
    			montanttotal = montanttotal + parseFloat(document.getElementById('total[' + a + '][3]').value);
     
    			}
    			alert ('montanttotal'+motanttotal);
    			//var total = document.createTextNode(totaltotal);
    			//var totalInput = document.createElement('input');
               // totalInput.setAttribute('type', 'hidden');
               // totalInput.setAttribute('name', 'total');
               // totalInput.setAttribute('value', total);
     
    			//td2.appendChild(total);
                //td2.appendChild(totalInput);
    A ce niveau:

    alert ('nom:'+embtotalInput.name+'valeur==>'+document.getElementById('total['+ a +'][3]').value);

    il me resort qu'il est null alors qu'il m'affiche correctement le motant dans l'alerte. Et ce la doit empecher de réaliser la suite du calcul.

    Quel erreur j'ai commise? Je n'arrive pas a comprendre

    Merci

    guigui69

  4. #4
    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 659
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 75
    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 659
    Billets dans le blog
    1
    Par défaut
    visiblement un souci de quotes dans l'alert ..
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  5. #5
    Rédacteur

    Avatar de Bovino
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Billets dans le blog
    20
    Par défaut
    Essaye :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    for (a=0;a<=nbLignes3;a++)
    			{
    			var elt_a = 'total['+a+'][3]';
    			alert ('nom:'+embtotalInput.name+'valeur==>'+document.getElementById(elt_a).value);
    			montanttotal = montanttotal + parseFloat(document.getElementById(elt_a).value);
     
    			}
    Pas de question technique par MP !
    Tout le monde peut participer à developpez.com, vous avez une idée, contactez-moi !
    Mes formations video2brain : La formation complète sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
    Mon livre sur jQuery
    Module Firefox / Chrome d'intégration de JSFiddle et CodePen sur le forum

  6. #6
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    829
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 829
    Par défaut
    Merci pour vos réponses.

    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
     
    			// Total par produit:
    			//alert ('test'+total);
    			//var embtotal = document.getElementById('pu_emb').value.replace(/(,)/gi, ".") * document.getElementById('qte_emb').value.replace(/(,)/gi, ".");
                var embtotal = document.createTextNode(total);
    			var embtotalInput = document.createElement('input');
                embtotalInput.setAttribute('type', 'hidden');
                embtotalInput.setAttribute('name', 'total[' + EMB.idEmballage + '][3]');
    			embtotalInput.setAttribute('id', 'total[' + EMB.idEmballage + '][3]');
                embtotalInput.setAttribute('value', total);
     
    			td5.appendChild(embtotal);
                td5.appendChild(embtotalInput);
    			alert('nom:'+embtotalInput.name);
    			alert('total:'+embtotalInput.value);
    			// Inc nbProduits et id
                EMB.nbEmballage++;
                EMB.idEmballage++;
     
    			//Calcul Complet total
    			var tr =  tbody.insertRow(tbody.rows.length);
    			var td1 = tr.insertCell(0);
    			var td2 = tr.insertCell(1);
    			var total = document.createTextNode('test');
    			td1.setAttribute('colSpan', 4);
    			td1.appendChild(total);
    			//var document.getElementById('total[0][3]').value = 0;
    			var montanttotal = 0;
    			var nbLignes3 = document.getElementById("ajout").rows.length;
    			alert ('nbre:'+nbLignes3);
    			for (a=0;a<=nbLignes3;a++)
    			{
    			var elt_a = 'total['+a+'][3]';
    			alert ('nom:'+embtotalInput.name+'valeur==>'+document.getElementById(elt_a).value);
    			montanttotal = montanttotal + parseFloat(document.getElementById(elt_a).value);
     
     
    			}
    			alert ('montanttotal'+motanttotal);
    			//var total = document.createTextNode(totaltotal);
    			//var totalInput = document.createElement('input');
               // totalInput.setAttribute('type', 'hidden');
               // totalInput.setAttribute('name', 'total');
               // totalInput.setAttribute('value', total);
     
    			//td2.appendChild(total);
                //td2.appendChild(totalInput);
    Mais il y a toujours le problème:

    firebug:
    document.getElementById(elt_a) is null
    [Break on this error] alert ('nom:'+embtotalInput.name+'val...'+document.getElementById(elt_a).value);

Discussions similaires

  1. suppression d'une ligne dans un tableau multidimensionnel
    Par ekinox62100 dans le forum Langage
    Réponses: 4
    Dernier message: 06/10/2010, 10h00
  2. Suppression d'une ligne dans un txt : via un tableau ?
    Par renaud26 dans le forum Langage
    Réponses: 4
    Dernier message: 13/09/2008, 18h49
  3. Réponses: 5
    Dernier message: 10/11/2006, 11h00
  4. [HTML]Débutant-Suppression d'une ligne dans un tableau
    Par Kotik dans le forum Balisage (X)HTML et validation W3C
    Réponses: 6
    Dernier message: 19/07/2006, 11h53
  5. probleme bizarre de hauteur auto d'une ligne dans un tableau
    Par zax-tfh dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 17/01/2005, 23h34

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