You are viewing limited content. For full access, please sign in.

Question

Question

Total in a Form

asked on October 28, 2015 Show version history

I have a table in a form that does calculation per row , i need to do a total of the "Total Price"

outside the table like it shows on the image.

At the moment it seems to be totaling the wrong inputs

$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.totaltotal').on('blur', 'input', sumtotal);

function sumtotal() {
var sum = 0; 
$('.cf-table-block tbody tr').each(function () {
var s = 0;
$(this).find('.price input').each(function () {
s += parseNumber($(this).val());
})
$(this).find('.subtotal input').val(s);
sum += s;
  var subtotal = $(this).find(".subtotal input").val();
  var unitPrice = $(this).find(".rate input").val();
  var totalPrice = subtotal * unitPrice;
$(this).find(".total input").val(totalPrice);
  
});
$(".totaltotal input").val(sum);
var subtotalprice = parseNumber($(".totaltotal input").val();
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

 
0 0

Answers

APPROVED ANSWER
replied on October 28, 2015 Show version history

Remove line 13

sum += s;

After you set the totalPrice variable on line 16, insert a new line underneath it and add

sum += parseNumber(totalPrice);

Now your .totaltotal input should have the correct value.

Note that this was edited so the sum checks for a valid number value.

0 0
SELECTED ANSWER
replied on October 29, 2015

Try replacing

sum += totalPrice;

with

sum += parseNumber(totalPrice);

0 0

Replies

replied on October 29, 2015

Its not working Alexander

heres the code 

$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.totaltotal').on('blur', 'input', sumtotal);

function sumtotal() {
var sum = 0; 
$('.cf-table-block tbody tr').each(function () {
var s = 0;
$(this).find('.price input').each(function () {
s += parseNumber($(this).val());
})
$(this).find('.subtotal input').val(s);
  var subtotal = $(this).find(".subtotal input").val();
  var unitPrice = $(this).find(".rate input").val();
  var totalPrice = subtotal * unitPrice;
  sum += totalPrice;
$(this).find(".total input").val(totalPrice);
  
  
});
$(".totaltotal input").val(sum);
var subtotalprice = parseNumber($(".totaltotal input").val());
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

0 0
SELECTED ANSWER
replied on October 29, 2015

Try replacing

sum += totalPrice;

with

sum += parseNumber(totalPrice);

0 0
replied on October 29, 2015

That's it it works , Thanks Alexander 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.