Suppresion d'une ligne dans un tableau

Eléphant du PHP | 440 Messages

19 nov. 2008, 19:01

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 : Tout sélectionner

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:

Code : Tout sélectionner

var tbody = document.getElementById('liste-emballage'); row = document.getElementById("ajout").rows.length; alert(row); document.getElementById('ajout').deleteRow(row); }
Merci

guigui69

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 10684 Messages

24 nov. 2008, 00:31

Bah l'erreur, javascript te la donne clairement (pour une fois :)) : "Index or size is negative or greater than the allowed amount". Tu lui passe un index qui est soit négatif, soit plus grand que la valeur max autorisée.

Comme tu as un alert(row) j'imagine que tu as constaté par toi même que l'index spécifié n'était pas négatif, il faut donc t'interroger sur le pourquoi la valeur que tu récupères ne lui convient pas.

Pour rappel, un tableau dont la longueur est de 10 éléments comporte des index allant de 0 à 9.... ;)
Ce n'est pas en améliorant la bougie que l'on a inventé l'ampoule...