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 :

faire la somme d'une colonne


Sujet :

JavaScript

  1. #1
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut faire la somme d'une colonne
    Bonjour,

    J'ai un souci a faire la somme d'une colonne de x nombre de ligne...Voici le code de mon formulaire:
    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
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Document sans titre</title>
    <script language="javascript">
    var index = 0;
    function loadData(valeur, indexLine) {
    	var data ;
    	var url = "ajax.php?reference=" + valeur;
    	if(window.XMLHttpRequest) // FIREFOX
    		xhr_object = new XMLHttpRequest();
    	else if(window.ActiveXObject) // IE
    		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    	else return(false);
    	xhr_object.open("GET", url, false);
    	xhr_object.send(null);
    	if(xhr_object.readyState == 4) {
    		//alert(xhr_object.responseText);
    		data = eval(xhr_object.responseText);
    		document.getElementById("designation[" + indexLine + "]").value = data[1];
    		val_tarif=document.getElementById("tarif[" + indexLine + "]").value = data[2];
    		document.getElementById("colis[" + indexLine + "]").value = data[3];	
    		document.getElementById("punet[" + indexLine + "]").value = data[2];
    		document.getElementById("total[" + indexLine + "]").value = data[2];
    	document.getElementById("montant_total").value = data[2];		
    	} else return(false);
    }
    function calcul_remise(value,indexLine){
    	var val_tarif=document.getElementById("tarif[" + indexLine + "]").value
    	var val_remise = document.getElementById("remise[" + indexLine + "]").value;
    	val_punet = val_tarif - (val_tarif*val_remise*1/100);
    document.getElementById("punet[" + indexLine + "]").value = val_punet;
    var quantite = document.getElementById("quantite[" + indexLine + "]").value;
     
    var prix_total = quantite * val_punet;
    document.getElementById("total[" + indexLine + "]").value = prix_total;
    document.getElementById("montant_total").value = prix_total;	
     
    //alert(val_tarif);
    	}
    	function calcul_quantite(value,indexLine){
    	var val_punet=document.getElementById("punet[" + indexLine + "]").value
    var quantite = document.getElementById("quantite[" + indexLine + "]").value;
     
    var prix_total = quantite * val_punet;
    document.getElementById("total[" + indexLine + "]").value = prix_total;
    //alert(val_tarif);
     
    	}
    function addLine() {
    	var newRow = document.getElementById('matable').insertRow(-1);
    	var reference = newRow.insertCell(0);
    	reference.innerHTML = '<input type="text" id="reference['+ index + ']" name="reference['+ index + ']" onChange="loadData(this.value, '+ index +')" />';
    	var designation = newRow.insertCell(1);
    	designation.innerHTML = '<input type="text" id="designation['+ index + ']"  name="designation['+ index + ']" readonly />';
    	var quantite = newRow.insertCell(2);
    	quantite.innerHTML = '<input type="text" id="quantite['+ index + ']" name="quantite['+ index + ']" value="1" onChange="calcul_quantite(this.value, '+ index +')" OnKeyUp = "somme()" />';
    	var colis = newRow.insertCell(3);
    	colis.innerHTML = '<input type="text" id="colis['+ index + ']" name="colis['+ index + ']" value="" />';
    	var remise = newRow.insertCell(4);
    	remise.innerHTML = '<input type="text" id="remise['+ index + ']" name="remise['+ index + ']" value="" onChange="calcul_remise(this.value, '+ index +')" />';
    	var tarif = newRow.insertCell(5);
    	tarif.innerHTML = '<input type="text" id="tarif['+ index + ']" name="tarif['+ index + ']" value="" readonly  />';
    	var punet = newRow.insertCell(6);
    	punet.innerHTML = '<input type="text" id="punet['+ index + ']" name="punet['+ index + ']" value="" />';
    	var total = newRow.insertCell(7);
    	total.innerHTML = '<input type="text" id="total['+ index + ']" name="total['+ index + ']" value="" />';
    	index++;
    }
    function deleteLine() {
    	var nb = document.getElementById('matable').rows.length;
    document.getElementById('matable').deleteRow(-1);
    }
    function somme(){
    	var n = index;
    	//alert(n);
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					parseInt(document.getElementById("montant_total").value)+
    					parseInt(document.getElementById("total[" + n + "]").value);
    			n++;
    		}
    }
    </script>
    </head>
     
    <body>
     
    <form method="post" action="commande.php">
    <table id="matable" width="1048" border="0">
     
      <tr>
        <td width="111"><strong>REFERENCE</strong></td>
        <td width="131"><strong>DESIGNATION</strong></td>
        <td width="66"><strong>Quantité</strong></td>
        <td width="94"><strong>COLIS</strong></td>
        <td width="138"><strong>Remise(%)</strong></td>
        <td width="157"><strong>TARIF(FCFA)</strong></td>
        <td width="143"><strong>PU NET (FCFA)</strong></td>
        <td width="174"><strong>TOTAL (FCFA)</strong></td>
      </tr>
     
    </table>
    <table width="1367" id="matable2">
      <tr>
        <td width="87"></td>
        <td width="103"></td>
        <td width="51"></td>
        <td width="73"></td>
        <td width="108"></td>
        <td width="124"></td>
        <td width="157"></td>
         <td width="291"></td>
        <td width="333"><strong>Montant Total:</strong><input type="text" id="montant_total" name="montont_total" value="" /></td>
      </tr>
    </table>
    <input type="button" name="button" id="button" value="Ajouter" onClick="addLine()" />
    <input type="button" name="button3" id="button3" value="Supprimer" onClick="deleteLine()" />
    <input type="submit" name="button2" id="button2" value="Valider" />
     
    </form>
    </body>
    </html>

  2. #2
    Inactif  

    Homme Profil pro
    développeur Vala
    Inscrit en
    Février 2011
    Messages
    478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Cher (Centre)

    Informations professionnelles :
    Activité : développeur Vala
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2011
    Messages : 478
    Points : 3 700
    Points
    3 700
    Par défaut
    dis moi si je me trompe, mais je ne vois qu'un id total[n], donc ta somme aura toujours la même valeur

  3. #3
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    L'id total[n] est la somme de chaque ligne et l'id montant_total est la somme total des total[n].je veux que a chaque fois qu'on change la quantité ou la remise que le montant_total change en fonction aussi

  4. #4
    Inactif  

    Homme Profil pro
    développeur Vala
    Inscrit en
    Février 2011
    Messages
    478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Cher (Centre)

    Informations professionnelles :
    Activité : développeur Vala
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2011
    Messages : 478
    Points : 3 700
    Points
    3 700
    Par défaut
    en fait, modifie ta fonction somme :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    function somme(){
    	var n = 0;
    document.getElementById("montant_total").value = 0;
    	//alert(n);
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					parseInt(document.getElementById("montant_total").value)+
    					parseInt(document.getElementById("total[" + n + "]").value);
    			n++;
    		}
    }
    ceci devrait fonctionner

  5. #5
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    Probléme résolu voici le code pour ceux qui aurant le méme probléme:

    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
    149
    150
    151
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Document sans titre</title>
    <script language="javascript">
    var index = 0;
    function loadData(valeur, indexLine) {
    	var data ;
    	var url = "ajax.php?reference=" + valeur;
    	if(window.XMLHttpRequest) // FIREFOX
    		xhr_object = new XMLHttpRequest();
    	else if(window.ActiveXObject) // IE
    		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    	else return(false);
    	xhr_object.open("GET", url, false);
    	xhr_object.send(null);
    	if(xhr_object.readyState == 4) {
    		//alert(xhr_object.responseText);
    		data = eval(xhr_object.responseText);
    		document.getElementById("designation[" + indexLine + "]").value = data[1];
    		val_tarif=document.getElementById("tarif[" + indexLine + "]").value = data[2];
    		document.getElementById("colis[" + indexLine + "]").value = data[3];	
    		document.getElementById("punet[" + indexLine + "]").value = data[2];
    		document.getElementById("total[" + indexLine + "]").value = data[2];
    	var n = 0;
    	//alert(n);
    	document.getElementById("montant_total").value = 0;
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					parseInt(document.getElementById("montant_total").value)+
    					parseInt(document.getElementById("total[" + n + "]").value);
    			n++;
    		}	
     
    	} else return(false);
    }
    function calcul_remise(value,indexLine){
    	var val_tarif=document.getElementById("tarif[" + indexLine + "]").value
    	var val_remise = document.getElementById("remise[" + indexLine + "]").value;
    	val_punet = val_tarif - (val_tarif*val_remise*1/100);
    document.getElementById("punet[" + indexLine + "]").value = val_punet;
    var quantite = document.getElementById("quantite[" + indexLine + "]").value;
     
    var prix_total = quantite * val_punet;
    document.getElementById("total[" + indexLine + "]").value = prix_total;
    document.getElementById("montant_total").value = prix_total;	
     
    //alert(val_tarif);
    	}
    	function calcul_quantite(value,indexLine){
    	var val_punet=document.getElementById("punet[" + indexLine + "]").value
    var quantite = document.getElementById("quantite[" + indexLine + "]").value;
     
    var prix_total = quantite * val_punet;
    document.getElementById("total[" + indexLine + "]").value = prix_total;
    //alert(val_tarif);
    	var n = 0;
    	//alert(n);
    	document.getElementById("montant_total").value = 0;
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					parseInt(document.getElementById("montant_total").value)+
    					parseInt(document.getElementById("total[" + n + "]").value);
    			n++;
    		}
    	}
    function addLine() {
    	var newRow = document.getElementById('matable').insertRow(-1);
    	var reference = newRow.insertCell(0);
    	reference.innerHTML = '<input type="text" id="reference['+ index + ']" name="reference['+ index + ']" onChange="loadData(this.value, '+ index +')" />';
    	var designation = newRow.insertCell(1);
    	designation.innerHTML = '<input type="text" id="designation['+ index + ']"  name="designation['+ index + ']" readonly />';
    	var quantite = newRow.insertCell(2);
    	quantite.innerHTML = '<input type="text" id="quantite['+ index + ']" name="quantite['+ index + ']" value="1" onChange="calcul_quantite(this.value, '+ index +')"  />';
    	var colis = newRow.insertCell(3);
    	colis.innerHTML = '<input type="text" id="colis['+ index + ']" name="colis['+ index + ']" value="" />';
    	var remise = newRow.insertCell(4);
    	remise.innerHTML = '<input type="text" id="remise['+ index + ']" name="remise['+ index + ']" value="" onChange="calcul_remise(this.value, '+ index +')" />';
    	var tarif = newRow.insertCell(5);
    	tarif.innerHTML = '<input type="text" id="tarif['+ index + ']" name="tarif['+ index + ']" value="" readonly  />';
    	var punet = newRow.insertCell(6);
    	punet.innerHTML = '<input type="text" id="punet['+ index + ']" name="punet['+ index + ']" value="" />';
    	var total = newRow.insertCell(7);
    	total.innerHTML = '<input type="text" id="total['+ index + ']" name="total['+ index + ']" value="" />';
    	index++;
    }
    function deleteLine() {
    	var nb = document.getElementById('matable').rows.length;
    document.getElementById('matable').deleteRow(-1);
    }
    /*function somme(){
    	var n = 0;
    	//alert(n);
    	document.getElementById("montant_total").value = 0;
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					parseInt(document.getElementById("montant_total").value)+
    					parseInt(document.getElementById("total[" + n + "]").value);
    			n++;
    		}
    }*/
    </script>
    </head>
     
    <body>
     
    <form method="post" action="commande.php">
    <table id="matable" width="1048" border="0">
     
      <tr>
        <td width="111"><strong>REFERENCE</strong></td>
        <td width="131"><strong>DESIGNATION</strong></td>
        <td width="66"><strong>Quantité</strong></td>
        <td width="94"><strong>COLIS</strong></td>
        <td width="138"><strong>Remise(%)</strong></td>
        <td width="157"><strong>TARIF(FCFA)</strong></td>
        <td width="143"><strong>PU NET (FCFA)</strong></td>
        <td width="174"><strong>TOTAL (FCFA)</strong></td>
      </tr>
     
    </table>
    <table width="1367" id="matable2">
      <tr>
        <td width="87"></td>
        <td width="103"></td>
        <td width="51"></td>
        <td width="73"></td>
        <td width="108"></td>
        <td width="124"></td>
        <td width="157"></td>
         <td width="291"></td>
        <td width="333"><strong>Montant Total:</strong><input type="text" id="montant_total" name="montont_total" value="" /></td>
      </tr>
    </table>
    <input type="button" name="button" id="button" value="Ajouter" onClick="addLine()" />
    <input type="button" name="button3" id="button3" value="Supprimer" onClick="deleteLine()" />
    <input type="submit" name="button2" id="button2" value="Valider" />
     
    </form>
    </body>
    </html>
     
    </html>

  6. #6
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    merci à rotrevrep!!!

  7. #7
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    voila je suis entrain de faire un script qui ajoute et supprime une ligne d'un tableau contenant des champs input et fait le total d'une colonne tout marche sauf que quand je supprime une ligne et en rajoute une autre il ne voit pas la derniére ligne ajoutée. voici mon script
    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
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="media/style.css">
    <title>Document sans titre</title>
    <script language="javascript">
    var index = 1;
    function loadData(valeur, indexLine) {
    	var data ;
    	var url = "ajax.php?reference=" + valeur;
    	if(window.XMLHttpRequest) // FIREFOX
    		xhr_object = new XMLHttpRequest();
    	else if(window.ActiveXObject) // IE
    		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    	else return(false);
    	xhr_object.open("GET", url, false);
    	xhr_object.send(null);
    	if(xhr_object.readyState == 4) {
    		//alert(xhr_object.responseText);
    		data = eval(xhr_object.responseText);
    		document.getElementById("designation[" + indexLine + "]").value = data[1];
    		val_tarif=document.getElementById("tarif[" + indexLine + "]").value = data[2];
    		document.getElementById("colis[" + indexLine + "]").value = data[3];	
    		document.getElementById("punet[" + indexLine + "]").value = data[2];
    		document.getElementById("total[" + indexLine + "]").value = data[2];
    	var n = 0;
    	//alert(n);
    	document.getElementById("montant_total").value = 0;
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					(parseFloat(document.getElementById("montant_total").value)+
    					parseFloat(document.getElementById("total[" + n + "]").value)).toFixed(2);
    			n++;
    		}	
     
    	} else return(false);
    }
    function calcul_remise(value,indexLine){
    	var val_tarif=document.getElementById("tarif[" + indexLine + "]").value
    	var val_remise = document.getElementById("remise[" + indexLine + "]").value;
    	val_punet = val_tarif - (val_tarif*val_remise*1/100);
    document.getElementById("punet[" + indexLine + "]").value = val_punet;
    var quantite = document.getElementById("quantite[" + indexLine + "]").value;
     
    var prix_total = quantite * val_punet;
    document.getElementById("total[" + indexLine + "]").value = prix_total;
    var n = 0;
    	//alert(n);
    	document.getElementById("montant_total").value = 0;
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					(parseFloat(document.getElementById("montant_total").value)+
    					parseFloat(document.getElementById("total[" + n + "]").value)).toFixed(2);
    			n++;
    		}
     
    //alert(val_tarif);
    	}
    	function calcul_quantite(value,indexLine){
    	var val_punet=document.getElementById("punet[" + indexLine + "]").value
    var quantite = document.getElementById("quantite[" + indexLine + "]").value;
     
    var prix_total = quantite * val_punet;
    document.getElementById("total[" + indexLine + "]").value = prix_total;
    //alert(val_tarif);
    	var n = 0;
    	//alert(n);
    	document.getElementById("montant_total").value = 0;
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					(parseFloat(document.getElementById("montant_total").value)+
    					parseFloat(document.getElementById("total[" + n + "]").value)).toFixed(2);
    			n++;
    		}
    	}
    function addLine() {
    	var newRow = document.getElementById('matable').insertRow(-1);
    	var reference = newRow.insertCell(0);
    	reference.innerHTML = '<input type="text" id="reference['+ index + ']" name="reference['+ index + ']" onChange="loadData(this.value, '+ index +')" />';
    	var designation = newRow.insertCell(1);
    	designation.innerHTML = '<input type="text" id="designation['+ index + ']"  name="designation['+ index + ']" readonly />';
    	var quantite = newRow.insertCell(2);
    	quantite.innerHTML = '<input type="text" id="quantite['+ index + ']" name="quantite['+ index + ']" value="1" onChange="calcul_quantite(this.value, '+ index +')"  />';
    	var colis = newRow.insertCell(3);
    	colis.innerHTML = '<input type="text" id="colis['+ index + ']" name="colis['+ index + ']" value="" />';
    	var remise = newRow.insertCell(4);
    	remise.innerHTML = '<input type="text" id="remise['+ index + ']" name="remise['+ index + ']" value="" onChange="calcul_remise(this.value, '+ index +')" />';
    	var tarif = newRow.insertCell(5);
    	tarif.innerHTML = '<input type="text" id="tarif['+ index + ']" name="tarif['+ index + ']" value="" readonly  />';
    	var punet = newRow.insertCell(6);
    	punet.innerHTML = '<input type="text" id="punet['+ index + ']" name="punet['+ index + ']" value="" />';
    	var total = newRow.insertCell(7);
    	total.innerHTML = '<input type="text" id="total['+ index + ']" name="total['+ index + ']" value="" />';
    	index++;
    }
    function deleteLine() {
     
    	var nb = document.getElementById('matable').rows.length;
    	if(nb > 2){
    		//alert(nb);
    document.getElementById('matable').deleteRow(-1);
    	}
    var n = 0;
    	//alert(n);
    	document.getElementById("montant_total").value = 0;
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					(parseFloat(document.getElementById("montant_total").value)+
    					parseFloat(document.getElementById("total[" + n + "]").value)).toFixed(2);
    			n++;
    		}
     
    	alert(n);
     
    }
    /*function somme(){
    	var n = 0;
    	//alert(n);
    	document.getElementById("montant_total").value = 0;
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					parseInt(document.getElementById("montant_total").value)+
    					parseInt(document.getElementById("total[" + n + "]").value);
    			n++;
    		}
    }*/
    </script>
    </head>
     
    <body>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <form method="post" action="commande.php">
     
    <table id="matable" width="1048" border="0">
     
      <tr>
        <td width="111"><strong>REFERENCE</strong></td>
        <td width="131"><strong>DESIGNATION</strong></td>
        <td width="66"><strong>Quantité</strong></td>
        <td width="94"><strong>COLIS</strong></td>
        <td width="138"><strong>Remise(%)</strong></td>
        <td width="157"><strong>TARIF(FCFA)</strong></td>
        <td width="143"><strong>PU NET (FCFA)</strong></td>
        <td width="174"><strong>TOTAL (FCFA)</strong></td>
      </tr>
       <tr>
        <td width="111"><input type="text" id="reference[0]" name="reference[0]" onChange="loadData(this.value, 0)" /></td>
        <td width="131"><input type="text" id="designation[0]"  name="designation[0]" readonly /></td>
        <td width="66"><input type="text" id="quantite[0]" name="quantite[0]" value="1" onChange="calcul_quantite(this.value, 0)"  /></td>
        <td width="94"><input type="text" id="colis[0]" name="colis[0]" value="" /></td>
        <td width="138"><input type="text" id="remise[0]" name="remise[0]" value="" onChange="calcul_remise(this.value, 0)" /></td>
        <td width="157"><input type="text" id="tarif[0]" name="tarif[0]" value="" readonly  /></td>
        <td width="143"><input type="text" id="punet[0]" name="punet[0]" value="" /></td>
        <td width="174"><input type="text" id="total[0]" name="total[0]" value="" /></td>
      </tr>
     
    </table>
    <table width="1367" id="matable2">
      <tr>
        <td width="87"></td>
        <td width="103"></td>
        <td width="51"></td>
        <td width="73"></td>
        <td width="108"></td>
        <td width="124"></td>
        <td width="157"></td>
         <td width="291"></td>
        <td width="333"><strong>Montant Total:</strong><input type="text" id="montant_total" name="montont_total" value="" /></td>
      </tr>
    </table>
    <input type="button" name="button" id="button" value="Ajouter" onClick="addLine()" />
    <input type="button" name="button3" id="button3" value="Supprimer" onClick="deleteLine()" />
    <input type="submit" name="button2" id="button2" value="Valider" />
     
    </form>
    </body>
    </html>
     
    </html>

  8. #8
    Inactif  

    Homme Profil pro
    développeur Vala
    Inscrit en
    Février 2011
    Messages
    478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Cher (Centre)

    Informations professionnelles :
    Activité : développeur Vala
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2011
    Messages : 478
    Points : 3 700
    Points
    3 700
    Par défaut
    pourrait tu rajouter ton ficher ajax.php ?
    Edit : en fait, je viens de tester ton script, et il fonctionne correctement, il met bien à jour le total à l'ajout ou à la suppression. Si il y a un autre problème, dis le.

  9. #9
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    au fait le probléme vient de la fonction deleteLine() physiquement il ne supprime, pas la ligne parce quand je refait un ajout le nombre de ligne ne s'incrémente.
    Par exemple si j'ai 4 lignes je supprime il devrait me rester 3 ligne mais quand je refaits un ajout au lieu d'avoir 4 j'ai 3.
    j'ai fait un alert sur la fonction loadData() et deleteline() comme suit
    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
     
    /***fonction loaData****/
    function loadData(valeur, indexLine) {
    	var data ;
    	var url = "ajax.php?reference=" + valeur;
    	if(window.XMLHttpRequest) // FIREFOX
    		xhr_object = new XMLHttpRequest();
    	else if(window.ActiveXObject) // IE
    		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    	else return(false);
    	xhr_object.open("GET", url, false);
    	xhr_object.send(null);
    	if(xhr_object.readyState == 4) {
    		//alert(xhr_object.responseText);
    		data = eval(xhr_object.responseText);
    		document.getElementById("designation[" + indexLine + "]").value = data[1];
    		val_tarif=document.getElementById("tarif[" + indexLine + "]").value = data[2];
    		document.getElementById("colis[" + indexLine + "]").value = data[3];	
    		document.getElementById("punet[" + indexLine + "]").value = data[2];
    		document.getElementById("total[" + indexLine + "]").value = data[2];
    	var n = 0;
    	//alert(n);
    	document.getElementById("montant_total").value = 0;
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					(parseFloat(document.getElementById("montant_total").value)+
    					parseFloat(document.getElementById("total[" + n + "]").value)).toFixed(2);
    			n++;
    		}
    		alert(n);	
     
    	} else return(false);
    }
    /***supprime ligne****/
    function deleteLine() {
     
    	var nb = document.getElementById('matable').rows.length;
    	if(nb > 2){
    		//alert(nb);
    document.getElementById('matable').deleteRow(-1);
    	}
    var n = 0;
    	//alert(n);
    	document.getElementById("montant_total").value = 0;
    while ( document.getElementById("total[" + n + "]") )
    		{	
    		//alert(document.getElementById("total[" + n + "]"));
    		document.getElementById("montant_total").value=
    					(parseFloat(document.getElementById("montant_total").value)+
    					parseFloat(document.getElementById("total[" + n + "]").value)).toFixed(2);
    			n++;
    		}
     
    	alert(n);
     
    }
    voici mon fichier ajax.php
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    <?php
    $reference = $_GET["reference"];
     
     
    include "connexion/connexion.php";
     
    $bd_connexion = mysql_select_db($database_connexion);
    $insertSQL = sprintf("select code_article,designation,colisage,prix_unitaire from articles where code_article='$reference';");
    $query = mysql_query($insertSQL,$connexion) or die(mysql_error());
     
    $row = mysql_fetch_row($query);
    echo "new Array('".$row[0]."', '".$row[1]."', '".$row[3]."','".$row[2]."')";
    ?>

  10. #10
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    as-tu essayé de supprimer et de rajouter?

  11. #11
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    17 082
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 17 082
    Points : 44 698
    Points
    44 698
    Par défaut
    Bonjour,
    juste un truc qui surprend, tu incrémentes un compteur, index, lors des ajouts mais tu ne décrémentes pas celui ci lors des suppressions .

  12. #12
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    merci tu as parfaitement raison et ça régle mon probléme

    grand merci

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

Discussions similaires

  1. Faire la somme d'une colonne obtenue avec la fonction SUM()
    Par gouzou38 dans le forum Langage SQL
    Réponses: 3
    Dernier message: 14/05/2011, 10h01
  2. Faire la somme d'une colonne dans la même requête
    Par logiciel_const dans le forum SQL
    Réponses: 23
    Dernier message: 26/10/2010, 09h47
  3. Faire la somme d'une colonne
    Par vg-matrix dans le forum Rave
    Réponses: 1
    Dernier message: 02/11/2009, 15h28
  4. faire la somme d'une colonne ou il y a des chifres et du texte
    Par newcodeur dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 03/07/2008, 14h28
  5. faire la somme d'une colonne d'une listbox
    Par Namson dans le forum VB 6 et antérieur
    Réponses: 7
    Dernier message: 25/04/2007, 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