Calcul d'une somme dans un tableau
Bonjour,
je fais appel à vous pour m'aider dans l'élaboration d'un bon de commande.
Pour l'instant j'ai un tableau avec les champs prix et quantité qui se calcul automatiquement.
Voici le code:
Code:
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
| window.onload = dtableInit;
/* initialise le script */
function dtableInit() {
var table = document.getElementsByTagName('TABLE');
for ( var i = 0; i < table.length; i++ ) {
// on récupère tous les tableaux dynamiques
if ( table[i].className = 'dTable' ) {
var tbody = table[i].tBodies[0];
var newTr = tbody.rows[0].cloneNode(true);
// on masque la première ligne du tbody (la ligne de reference)
tbody.rows[0].style.display = 'none';
// on en ajoute une
tbody.appendChild(newTr);
}
}
}
/* trouve le tag "parentTagName" parent de "element" */
function getParent(element, parentTagName) {
if ( ! element )
return null;
else if ( element.nodeType == 1 && element.tagName.toLowerCase() == parentTagName.toLowerCase() )
return element;
else
return getParent(element.parentNode, parentTagName);
}
/* ajoute une ligne */
function addLigne(link) {
// 1. récuperer le node "TABLE" à manipuler
var td = link.parentNode;
var table = getParent(td,'TABLE');
// 2. on va manipuler le TBODY
var tbody = table.tBodies[0];
// 3. on clone la ligne de reference
var newTr = tbody.rows[0].cloneNode(true);
var newTr = tbody.rows[0].cloneNode(true)
tbody.appendChild(newTr);
if ( document.all ) // pour IE
newTr.style.display = "block";
else
newTr.style.display = "table-row"; // pour Gecko
}
/* supprimer une ligne */
function delLigne(link) {
// 1. récuperer le node "TABLE" à manipuler
var td = link.parentNode;
var table = getParent(td, 'TABLE');
// 2. récuperer le TBODY
var tbody = table.tBodies[0];
// 3. Supprimer le TR
tbody.removeChild(getParent(td, 'TR'));
}
function calcul(obj){
// recherche TD parente de l'input (obj) passé en paramètre
var oTD = obj.parentNode;
// recherche TR parente de la TD
var oTR = oTD.parentNode;
// récup de la position de la TD sur la ligne
var ind = oTD.cellIndex;
// récup. TD précédente
var oTDavant = oTR.cells[ind-1];
// récup. TD suivante
var oTDapres = oTR.cells[ind+1];
// récup de l'INPUT de la TD précédente
var oInputAvant = oTDavant.firstChild;
// récup. de l'INPUT de la TD suivante
var oInputApres = oTDapres.firstChild;
// OUF!!! enfin le calcul
oInputApres.value = oInputAvant.value * obj.value;
}
function calcul2(obj){
// recherche TD parente de l'input (obj) passé en paramètre
var oTD = obj.parentNode;
// recherche TR parente de la TD
var oTR = oTD.parentNode;
// récup de la position de la TD sur la ligne
var ind = oTD.cellIndex;
// récup. TD précédente
var oTDapres = oTR.cells[ind+1];
// récup. TD suivante
var oTDapres2 = oTR.cells[ind+2];
// récup de l'INPUT de la TD précédente
var oInputapres = oTDapres.firstChild;
// récup. de l'INPUT de la TD suivante
var oInputApres2 = oTDapres2.firstChild;
// OUF!!! enfin le calcul
oInputApres2.value = oInputapres.value * obj.value;
} |
Code:
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
| <body>
<table class="dTable" style="text-align:center; width:90%; margin-top:30px;">
<thead>
<tr>
<th style="text-align:center; width:5%">Poste <br/>analytique</th>
<th style="text-align:center; width:5%">Code <br/>chantier</th>
<th style="text-align:center; width:40%">Désignation</th>
<th style="text-align:center; width:5%">Unité</th>
<th style="text-align:center; width:5%">Quantité</th>
<th style="text-align:center; width:10%">Prix unitaire <br/>H.T.</th>
<th style="text-align:center; width:10%">Montant H.T.</th>
<th style="text-align:center; width:5%">Taux <br/>TVA</th>
<th></th>
</tr>
</thead>
<tbody id="mon_tableau">
<tr>
<td><input class = "tableau" type = "text" name = "champ1[]" /></td>
<td><input class = "tableau" type = "text" name = "champ2[]" /></td>
<td><input class = "tableau" type = "text" name = "champ3[]" /></td>
<td><input class = "tableau" type = "text" name = "champ4[]" /></td>
<td><input class = "tableau" type = "text" name = "champ5[]" onkeyup = "calcul2(this);"/></td>
<td><input class = "tableau" type = "text" name = "champ6[]" onkeyup = "calcul(this);"/> </td>
<td><input class = "tableau" type = "text" name = "champ7[]"/></td>
<td><input class = "tableau" type = "text" name = "champ8[]" /></td>
<td><a href="#" onclick="delLigne(this); return false;">Supp</a></td>
</tr>
</tbody>
<tfoot>
<tr><td colspan="3" style="text-align:left"><p>voir devis joint</p></td><td colspan="3" style="text-align:left; font-weight:bold;">
<FONT size="2pt">Montant total HT :</FONT></td><td colspan="3"><h3><input class="tableau" type="text" name="mont_HT" value="" id="mont_HT"/></h3></td></tr>
<td colspan="3" rowspan="2"><p>... </p></td><td rowspan="1" colspan="3" style="text-align:left; font-weight:bold;">
<tr><th colspan="9"><a href="#" onclick="addLigne(this);addLigne(this);addLigne(this);addLigne(this);addLigne(this);addLigne(this);addLigne(this);addLigne(this);addLigne(this);addLigne(this); return false;">Ajouter une ligne</a></th></tr>
<a href="#" class="bouton empty blanc" style="float:right;margin:-200px 0; z-index:2; position:fixed" onclick="javascript:document.insertion.submit();">Enregistrer</a>
</tfoot>
</table> |
J'aimerais que le montant total soit calculé automatiquement lors de la saisie.
Pouvait vous m'aidez s'il vous plait? :calim2: