2017-11-20 3 views
-2

Ich habe einige Zeile mit einigen Produktdaten.Ich kann mehr Zeile dynamisch hinzufügen von jQuery, Ich habe die Summe für jede Zeile am Ende berechnen. Jetzt möchte ich die Gesamtsumme aller Zeilen berechnen. Ich habe müde, aber es ist immer die letzte Zeile insgesamt in der Gesamtsumme anzeigen. sehen Sie bitte das Bild.So berechnen Sie die Summe aller Zeilen in jquery

enter image description here

Hier ist mein jquery Teil.

//calculation here 

$('#dsp').on('input','.ctn',function(){ 
    var cal=$(this).val(); 
    var gparent=$(this).closest('.row'); 
    var unitp=gparent.find('.u_price').val(); 
    var unitpctn=gparent.find('.unit_pctn').val(); 
    var pcs=gparent.find('.pcs').val(); 
    //alert(pcs); 
    var total=(((parseInt(unitpctn)*parseInt(cal)) + parseInt(pcs))*parseInt(unitp)); 

     gparent.find('.t_amt').val(total); 

     //grand total 
     var gtotal=0; 
     var gtotal=parseInt(gtotal)+parseInt(total) 
     //alert(gtotal); 
     $('#tot').html(gtotal); 


}); 

$('#dsp').on('input','.pcs',function(){ 
    var pcs=$(this).val(); 
    var gparent=$(this).closest('.row'); 
    var unitp=gparent.find('.u_price').val(); 
    var unitpctn=gparent.find('.unit_pctn').val(); 
    var ctn=gparent.find('.ctn').val(); 
    //alert(pcs); 
    var total=(((parseInt(unitpctn)*parseInt(ctn)) + parseInt(pcs))*parseInt(unitp)); 

     gparent.find('.t_amt').val(total); 

     //grand total 
     var gtotal=0; 
     var gtotal=parseInt(gtotal)+parseInt(total) 
     //alert(gtotal); 
     $('#tot').html(gtotal); 


}); 

Hier ist meine Ansicht Teil.

<div id="dsp"> 
    <div class="row"> 
    <div class="col-md-2"> 
     <select name="p_name[]" class="form-control p_name"> 
     <option value="">-Select Product-</option> 
     @foreach($products as $product) 
     <option value="{{$product->product_id}}">{{$product->name}}</option> 
     @endforeach 
    </select> 
    </div> 
    <div class="col-md-2"> 
     <input type="text" name="p_code[]" class="form-control p_code"> 
    </div> 
    <div class="col-md-2"> 
     <input type="text" name="unit_pctn[]" class="form-control unit_pctn" value="0"> 
    </div> 
    <div class="col-md-2"> 
     <input type="text" name="u_price[]" class="form-control u_price" value="0"> 
    </div> 
    <div class="col-md-1"> 
     <input type="text" name="ctn[]" class="form-control ctn" value="0"> 
    </div> 
    <div class="col-md-1"> 
     <input type="text" name="pcs[]" class="form-control pcs" value="0"> 
    </div> 
    <div class="col-md-2"> 
     <input type="text" name="t_amt[]" class="form-control t_amt" value="0"> 
    </div> 

    </div>&nbsp; 
</div> 
      <div class="row" id="nep"></div> 
      <div class="row"> 
         <div class="col-md-10"></div> 
        <div class="col-md-2">GrandTotal:<h5 id="tot">0</h5></div> 
        </div> 
+1

Schnipsel ist kaputt. Bitte überprüfen –

Antwort

2

Hier ist eine kleine Funktion, die Sie berechnen die Gesamtsumme helfen:

function getGrandTotal() { 
    var total = 0; 
    $(".t_amt").each(function() { 
    total += parseFloat($(this).val()) || 0; 
    }); 
    return total; 
} 

Sie oben Funktion auf change einzelner insgesamt OR an der Aktualisierung einzelnen Summen aufrufen müssen. Rufen Sie einfach die Funktion auf, und Sie erhalten die Gesamtsumme zurück.

+0

ich es in jquery versucht haben, aber noch zeigen die letzte Summe in Grandtotal je $ (function() { var gtotal = 0; gtotal + = parseFloat ($ (this) ('t_amt.').. val()) || 0; $ ('# tot') .html (gtotal); }); – Majeed

+0

Weil Sie es falsch benutzt haben. 'var gtotal = 0;', diese Zeile darf nicht in 'each' sein, sonst erhalten Sie immer die letzte Summe. Ändern Sie es in 'var gtotal = 0; $ ('. t_amt'). je (function() {gtotal + = parseFloat ($ (this) .val()) || 0; $ ('# tot') .html (gtotal);}); ' –

+0

Danke, es funktioniert jetzt. – Majeed

0

Dieser Code ist Code Arbeit für Ihr Problem.

gparent.find(".t_amt").each(function() { 
       var gtotal=parseInt(gtotal)+parseInt(total) 
    } 

vollständige Code:

//calculation here 

    $('#dsp').on('input','.ctn',function(){ 
     var cal=$(this).val(); 
     var gparent=$(this).closest('.row'); 
     var unitp=gparent.find('.u_price').val(); 
     var unitpctn=gparent.find('.unit_pctn').val(); 
     var pcs=gparent.find('.pcs').val(); 
     //alert(pcs); 
     var total=(((parseInt(unitpctn)*parseInt(cal)) + parseInt(pcs))*parseInt(unitp)); 

      gparent.find('.t_amt').val(total); 

      //grand total 
      var gtotal=0; 
      var gtotal=parseInt(gtotal)+parseInt(total) 
      //alert(gtotal); 
      $('#tot').html(gtotal); 


    }); 

    $('#dsp').on('input','.pcs',function(){ 
     var pcs=$(this).val(); 
     var gparent=$(this).closest('.row'); 
     var unitp=gparent.find('.u_price').val(); 
     var unitpctn=gparent.find('.unit_pctn').val(); 
     var ctn=gparent.find('.ctn').val(); 
     //alert(pcs); 
     var total=(((parseInt(unitpctn)*parseInt(ctn)) + parseInt(pcs))*parseInt(unitp)); 

      gparent.find('.t_amt').val(total); 

      //grand total 
      var gtotal=0; 
gparent.find(".t_amt").each(function() { 
      var gtotal=parseInt(gtotal)+parseInt(total); 
} 
      //alert(gtotal); 
      $('#tot').html(gtotal); 


    }); 
0

Daten binden und calcultion alle sind hier fertig,

$(document).ready(function(){ 
$('#dsp').on('change','.p_name',function(){ 
    var pid=$(this).val(); 
    var parent= $(this).closest('.row'); 
    //alert(pid); 
    $.ajax({ 
    url:"{{route('getinfo')}}", 
    method:'post', 
    data:{id:pid,'_token':"{{csrf_token()}}"}, 
    success:function(response) { 

     //alert(response.code); 
     parent.find('.p_code').val(response.code); 
     parent.find('.unit_pctn').val(response.pcs_per_ctn); 
    } 
    }); 

    $.ajax({ 
    url:"{{route('getprice')}}", 
    method:'post', 
    data:{id:pid,'_token':"{{csrf_token()}}"}, 
    success:function(response) { 
     //alert(response.code); 

     parent.find('.u_price').val(response.client_price); 

    } 
    }); 



}); 

//calculation here 

$('#dsp').on('input','.ctn',function(){ 
    var cal=$(this).val(); 
    var gparent=$(this).closest('.row'); 
    var unitp=gparent.find('.u_price').val(); 
    var unitpctn=gparent.find('.unit_pctn').val(); 
    var pcs=gparent.find('.pcs').val(); 
    //alert(pcs); 
    var total=(((parseInt(unitpctn)*parseInt(cal)) + parseInt(pcs))*parseInt(unitp)); 

     gparent.find('.t_amt').val(total); 

     //grand total 
     var gtotal=0; 
     var gtotal=parseInt(gtotal)+parseInt(total) 
     //alert(gtotal); 
     $('#tot').val(gtotal); 


}); 

$('#dsp').on('input','.pcs',function(){ 
    var pcs=$(this).val(); 
    var gparent=$(this).closest('.row'); 
    var unitp=gparent.find('.u_price').val(); 
    var unitpctn=gparent.find('.unit_pctn').val(); 
    var ctn=gparent.find('.ctn').val(); 
    //alert(pcs); 
    var total=(((parseInt(unitpctn)*parseInt(ctn)) + parseInt(pcs))*parseInt(unitp)); 

     gparent.find('.t_amt').val(total); 

     //grand total 

     var gtotal=0; 
     $('.t_amt').each(function(){ gtotal += parseFloat($(this).val()) || 0; $('#tot').val(gtotal); }); 
    }); 

});

Verwandte Themen