Iterate only one time over a table

Iterate only one time over a table

giulichajarigiulichajari Posts: 19Questions: 8Answers: 0

I have a Datatable with preico, cantidad, subtotal=precio * cantidad, alicuota(wich is a % tax), base=subtotal *(alicuota/100),

Alicuota can be 10.5%, 21% or 27%.

And what i need is:
- the sum of subtotal of each record of the table
- the sum of base (with tax of 10.5%), and the same for 21 and 27%

All are calculated fields as you see.

i have this code ina function:

       function calculaTotales(t){
       var subs=0;
       var base=0;
       var ali10=0;
       var ali21=0;
       var ali27=0;
        var subtotal=0;
         t=$("#factura").DataTable();
         var data = t.rows().data();
          data.each(function(datos,index){
         var data=datos.details;
       subtotal=subtotal+ (data.precio * data.cantidad);

           subs=subs +subtotal;
             console.log(subs.toFixed(2));

                base=subtotal * (data.alicuota/100);
          if (data.alicuota=="10.50"){
         ali10=ali10 + base;

                                                    }
           if (data.alicuota=="21.00"){
                  ali21=ali21 + base;

                                                     }
            if (data.alicuota=="27.00"){
               ali27=ali27+base;
                                                   }
                          });  

for example:

an in the console i have: "1080.80" as a sum of subtotal, probably i was thinking the function iterates over each row more than one time. I was trying iterate() function and is the same.

some solution?

Answers

  • giulichajarigiulichajari Posts: 19Questions: 8Answers: 0

    Well i have discovered the mistake:

     subtotal=subtotal+ (data.precio * data.cantidad);
    

    i change it for:

      subtotal=data.precio * data.cantidad;
    

    but now: is possible to pass a variable into field of DataTable? i want having other datatable with the sums. I tried it:

          totalesfact=  $('#totales').DataTable({
         "searching": false,
         "ordering": false,
          "bLengthChange": false,
         "bFilter": true,
           "bInfo": false,
         "bPaginate": false,
    
                columnDefs: [
                { type: "num", symbols:"$" , targets: 3 }
            ],
    
    
              columns: [
               {title: "NETO GRAVADO:",data:subs },
         ...others columns, 
    

    But it doesn't show anything.

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @giulichajari ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

This discussion has been closed.