0

I have a button of which voids the last tr in a table. Each time it does this the total is worked out again using function mainTotalCalc().

The total is worked out through using .each and classes.

The issue arises when i get to one last row remaining. The total will remain as the last total. It never reaches 0.00

Now i think i can see why, as the JQ each considers the last total in the variable.

I am stuck on how to make the last remaining row be voided whilst the total calculates correctly.

The Fiddle (But parseFloat does not seem to work here)

The JS/JQ

function mainTotalCalc() {
    var activetableID = $("li.till_table_button.active").attr('id');
     // alert(activetableID);
    var tablenumber = $("#"+activetableID).data("tableref");
     // alert(tablenumber); //testing

     var newtotal=0,total=0;

     $('.till__tablepanel_table_'+tablenumber+'_row__totalprice').each( function(i) {
      var number = $(this).text();
       //alert(content);
       //num = parseInt(number);
      num = parseFloat(number);
      newtotal += num;
       // alert(number); //testing

      totalpriceDecimal = newtotal.toFixed(2);
      $('#till__totalpanel_table_'+tablenumber+'_price').html(newtotal.toFixed(2));
       // alert(totalpriceDecimal); 
      i++;

     });

}

Before Last Void before void

After Last Void (Total remains as 1.00 and shouldn't be) after void

2
  • What triggers mainTotalCalc() to be fired? Commented Apr 21, 2014 at 17:02
  • its the last thing called in a function which initiates the tr:last removal. The removal works perfectly, and adding rows calculates perfectly. Its just when voiding them, the last one has an issue. I think its something to do with the +=, but i am not sure. Commented Apr 21, 2014 at 17:04

3 Answers 3

1

Set up a guard condition if there are no more rows:

if ($('.till__tablepanel_table_0_row__totalprice').length == 0)
  $('#till__totalpanel_table_0_price').html('0.00');

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

thats exactly what i was looking for, thankyou so much. I tried so many ways, but not this. Awesome!
I see the issue now as there were not any rows for the calculation to take place. This solves the issue perfectly. I just did not realise that no rows meant no calculation previously.
0

The issue with your code is that the total is being updated inside the .each loop.

If there are now rows to total, this will not run.

Modify your function as follows:

function mainTotalCalc() {
    $('.till__tablepanel_table_'+tablenumber+'_row__totalprice').each( function(i) {
      var number = $(this).text();
       //alert(content);
       //num = parseInt(number);
      num = parseFloat(number);
      newtotal += num;
       // alert(number); //testing

      totalpriceDecimal = newtotal.toFixed(2);
       // alert(totalpriceDecimal); 
      i++;

     });
     // Move this line outside of the .each loop
     $('#till__totalpanel_table_'+tablenumber+'_price').html(newtotal.toFixed(2));
}

1 Comment

i had tried this, and understand now the error. The actual rows do not exist to calculate a total. Thats why this approach did not work. Thanks though
0

You have missed newtotal variable declaration, simply do this:

function mainTotalCalc() {
    var newtotal = 0.0; // MISSED VARIABLE DECLARATION
    $('.till__tablepanel_table_0_row__totalprice').each( function() {         
      var number = $(this).text();
      console.log('number: ' + number);

      newtotal += parseFloat(number);          
      console.log('newtotal= ' + newtotal);

      totalpriceDecimal = newtotal.toFixed(2);
      $('#till__totalpanel_table_0_price').html(totalpriceDecimal);
      // alert(totalpriceDecimal);

     });   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.