Gestion événement dans un tableau
Bonjour à Tous,
Lorsque je récupère mon tableau pour le modifier le calcul en rapport à mes quantités ne se fait que sur la première ligne, sur les autre mon total reste inchangé si je modifie mes quantités.
Côté Php
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 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
| <div class='row'>
<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12'>
<table class="table table-bordered table-hover" >
<thead>
<tr>
<th width="2%"><input id="id" class="formcontrol" type="checkbox"/></th>
<th width="15%">Ref</th>
<th width="38%">Produits</th>
<th width="15%">Prix</th>
<th width="15%">Qtt</th>
<th width="15%">Total</th>
</tr>
</thead>
<?php
$query = "select * from subsciption WHERE invoice='$memid'";
$result = mysqli_query($con,$query);
if(mysqli_affected_rows($con)!=0){
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
?>
<tbody>
<td><input class="case" name="id[]" type="checkbox" value="<?php echo $row['id'];?>" /></td>
<td><input type="text" data-type="productCode" name="itemNo[]" id="itemNo_1" class="form-control autocomplete_txt" autocomplete="off" value="<?php echo $row['sub_type'];?>"></td>
<td><input type="text" data-type="productName" name="itemName[]" id="itemName_1" class="form-control autocomplete_txt" autocomplete="off" value="<?php echo $row['name_produit'];?>"></td>
<td><input type="number" name="price[]" id="price_1" class="form-control changesNo" value="<?php echo $row['prix_produit'];?>" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" ></td>
<td><input type="number" name="quantity[]" id="quantity_1" class="form-control changesNo" value="<?php echo $row['qtt'];?>" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" ></td>
<td><input type="number" name="total[]" id="total_1" class="form-control totalLinePrice" value="<?php echo $row['total_produit']?>" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" ></td>
</tbody>
<?php
}
}
?>
</table>
<div class='row' style="margin-left: 10px; margin-top: 10px;">
<div class='col-xs-12 col-sm-3 col-md-3 col-lg-3'>
<button class="btn btn-danger delete" type="button">- Supprimer</button>
<button class="btn btn-success addmore" type="button">+ Ajout produit</button>
</div>
<?php
$query = "select * from invoices WHERE invoice='$memid'";
$result = mysqli_query($con,$query);
if(mysqli_affected_rows($con)!=0){
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
?>
<div class='col-xs-12 col-sm-offset-4 col-md-offset-4 col-lg-offset-4 col-sm-5 col-md-5 col-lg-5'>
<form class="form-inline">
<div class="form-group">
<label>Sous Total: </label>
<div class="input-group">
<input type="number" class="form-control" name="subtotal" id="subTotal" placeholder="Sous Total" onkeypress="return IsNumeric(event);" ondrop="return false;" value="<?php echo $row['sous_total'];?>" onpaste="return false;">
<div class="input-group-addon"></div>
</div>
</div>
<div class="form-group">
<label>TVA: </label>
<div class="input-group">
<div class="input-group-addon">%</div>
<input type="number" class="form-control" id="tax" placeholder="TVA" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
</div>
</div>
<div class="form-group">
<label>Total TVA: </label>
<div class="input-group">
<input type="number" class="form-control" name="taxAmount" id="taxAmount" placeholder="TVA" onkeypress="return IsNumeric(event);" ondrop="return false;" value="<?php echo $row['tva'];?>" onpaste="return false;">
<div class="input-group-addon"></div>
</div>
</div>
<div class="form-group">
<label>Total: </label>
<div class="input-group">
<input type="number" class="form-control" name="totalAftertax" id="totalAftertax" placeholder="Total" value="<?php echo $row['total'];?>" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
<div class="input-group-addon"></div>
</div>
</div>
<div class="form-group">
<label>Paiement: </label>
<div class="input-group">
<input type="number" class="form-control" name="amountPaid" id="amountPaid" placeholder="Paiement" onkeypress="return IsNumeric(event);" ondrop="return false;" value="<?php echo $row['paid'];?>" onpaste="return false;">
<div class="input-group-addon"></div>
</div>
</div>
<div class="form-group">
<label>Solde: </label>
<div class="input-group">
<input type="number" class="form-control amountDue" name="amountDue" id="amountDue" placeholder="Solde" onkeypress="return IsNumeric(event);" ondrop="return false;" value="<?php echo $row['bal'];?>" onpaste="return false;">
<div class="input-group-addon"></div>
</div>
</div>
<div class="form-group">
<div class="controls"><span id="spryselect2">
<select name="m" id="id" data-rule-required="true" required>
<option value=""><?php echo $row['reglement'];?></option>
<option value="CB">CB</option>
<option value="Chèque">Chèque</option>
<option value="Espèces ">Espèces </option>
</select>
</div>
</div>
<?php
}
}
?>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Enregistrer</button>
</div>
</form>
</div>
</div> |
Côté js
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
| //autocomplete script
$(document).on('focus','.autocomplete_txt',function(){
type = $(this).data('type');
if(type =='productCode' )autoTypeNo=0;
if(type =='productName' )autoTypeNo=1;
$(this).autocomplete({
source: function( request, response ) {
var array = $.map(prices, function (item) {
var code = item.split("|");
return {
label: code[autoTypeNo],
value: code[autoTypeNo],
data : item
}
});
//appel au filtre
response($.ui.autocomplete.filter(array, request.term));
},
autoFocus: true,
minLength: 2,
select: function( event, ui ) {
var names = ui.item.data.split("|");
id_arr = $(this).attr('id');
id = id_arr.split("_");
$('#itemNo_'+id[1]).val(names[0]);
$('#itemName_'+id[1]).val(names[1]);
$('#quantity_'+id[1]).val(1);
$('#price_'+id[1]).val(names[2]);
$('#total_'+id[1]).val( 1*names[2] );
calculateTotal();
}
});
});
//Changement du prix
$(document).on('change keyup blur','.changesNo',function(){
id_arr = $(this).attr('id');
id = id_arr.split("_");
quantity = $('#quantity_'+id[1]).val();
price = $('#price_'+id[1]).val();
if( quantity!='' && price !='' ) $('#total_'+id[1]).val( (parseFloat(price)*parseFloat(quantity)).toFixed(2) );
calculateTotal();
});
$(document).on('change keyup blur','#tax',function(){
calculateTotal();
});
//calcul du prix
function calculateTotal(){
subTotal = 0 ; total = 0;
$('.totalLinePrice').each(function(){
if($(this).val() != '' )subTotal += parseFloat( $(this).val() );
});
$('#subTotal').val( subTotal.toFixed(2) );
tax = $('#tax').val();
if(tax != '' && typeof(tax) != "undefined" ){
taxAmount = subTotal * ( parseFloat(tax) /100 );
$('#taxAmount').val(taxAmount.toFixed(2));
total = subTotal + taxAmount;
}else{
$('#taxAmount').val(0);
total = subTotal;
}
$('#totalAftertax').val( total.toFixed(2) );
calculateAmountDue();
}
$(document).on('change keyup blur','#amountPaid',function(){
calculateAmountDue();
});
//calcul du solde
function calculateAmountDue(){
amountPaid = $('#amountPaid').val();
total = $('#totalAftertax').val();
if(amountPaid != '' && typeof(amountPaid) != "undefined" ){
amountDue = parseFloat(total) - parseFloat( amountPaid );
$('.amountDue').val( amountDue.toFixed(2) );
}else{
total = parseFloat(total).toFixed(2);
$('.amountDue').val( total);
}
}
//restriction non-chifre
var specialKeys = new Array();
specialKeys.push(8,46); //Backspace
function IsNumeric(e) {
var keyCode = e.which ? e.which : e.keyCode;
console.log( keyCode );
var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
return ret;
} |
J'ai bien toutes les lignes correspondantes à mon enregistrement dans ma bdd mais lors de changements dans la quantité l'action ne se fait que pour la première ligne.
Merci pour votre aide