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

Question

Question

calculations within and outside of a table in forms

asked on October 14, 2015

I am doing calculations in a table in forms, the form is an adaption of the form on the Laserfiche forms help site.

What the form currently does is add the two row entries and provide a row total, I then need a subtotal of all row totals at the bottom of the form, then i need the GST to show,( as a calculation of the subtotal multiplied by .15 )

Then finally the total will be the GST value added to the subtotal.

0 0

Answer

SELECTED ANSWER
replied on October 15, 2015

Your JS just needs a slight modification to get things working. Here's the fixed code:

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

    function sumtotal() {
        var sum = 0;
        $('.cf-table-block tbody tr').each(function () {
            var s = 0;
            s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
            $(this).find('.subtotal input').val(s);
            sum += s;

        });
        $(".shipping input").val(sum);
        var subtotalPrice = parseNumber($(".shipping input").val());
        var taxAmount = parseNumber((subtotalPrice * 0.15).toFixed(2));
        $(".tax input").val(taxAmount);
        $(".total input").val((taxAmount + subtotalPrice).toFixed(2));
    }

    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

When you are setting the shipping input value, you need to use the sum variable and not s. Also, I'd recommend using toFixed(2) so you don't get long running decimal values

1 0

Replies

replied on October 15, 2015

Hi there Ian,

To get your totals working it's probably easiest to remove line 12 in the code above altogether, then replace line 14 with:

$(".shipping input").val(s);
var subtotalPrice = $(".shipping input").val();
var taxAmount = subtotalPrice * 0.15;
$(".tax input").val(taxAmount);
$(".total input").val(taxAmount + subtotalPrice);

Hope this helps!

Dan

0 0
replied on October 15, 2015

That doesn't seem to work Dan, the Subtotal GST and Total Input boxes are empty.

Hers is the code i used

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

    function sumtotal() {
        var sum = 0;
        $('.cf-table-block tbody tr').each(function () {
            var s = 0;
            s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
            $(this).find('.subtotal input').val(s);
            sum += s;

        });
        $(".shipping input").val(s);
        var subtotalPrice = $(".shipping input").val();
        var taxAmount = subtotalPrice * 0.15;
        $(".tax input").val(taxAmount);
        $(".total input").val(taxAmount + subtotalPrice);
    }

    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

 

replied on October 15, 2015

That doesn't work for me Dan, the GST , subtotals and Totals are all just empty

Here's the Code

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

    function sumtotal() {
        var sum = 0;
        $('.cf-table-block tbody tr').each(function () {
            var s = 0;
            s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
            $(this).find('.subtotal input').val(s);
            sum += s;

        });
        $(".shipping input").val(s);
        var subtotalPrice = $(".shipping input").val();
        var taxAmount = subtotalPrice * 0.15;
        $(".tax input").val(taxAmount);
        $(".total input").val(taxAmount + subtotalPrice);
    }

    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 15, 2015

Your JS just needs a slight modification to get things working. Here's the fixed code:

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

    function sumtotal() {
        var sum = 0;
        $('.cf-table-block tbody tr').each(function () {
            var s = 0;
            s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
            $(this).find('.subtotal input').val(s);
            sum += s;

        });
        $(".shipping input").val(sum);
        var subtotalPrice = parseNumber($(".shipping input").val());
        var taxAmount = parseNumber((subtotalPrice * 0.15).toFixed(2));
        $(".tax input").val(taxAmount);
        $(".total input").val((taxAmount + subtotalPrice).toFixed(2));
    }

    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

When you are setting the shipping input value, you need to use the sum variable and not s. Also, I'd recommend using toFixed(2) so you don't get long running decimal values

1 0
replied on October 15, 2015

That worked great thank you smiley

0 0
replied on October 16, 2015

Ahh yes sorry, rookie mistake on my behalf! wink Cheers Alex

0 0
replied on October 1, 2018 Show version history

so I am calculating time and so far the code is only recognizing the the hours in my 24hr format but not recognizing the minuets column making my calculation continue to come out with whole #s

0 0
replied on October 1, 2018 Show version history

Hi Jeremiah, you might be best to post this as a new post rather than in here, just got to the Laserfiche Forms Topics, scroll down to the bottom of the page to create a new post.

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

Sign in to reply to this post.