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

Question

Question

Roundtrip Checkbox on Mileage Form

asked on March 19, 2014

I have a mileage reimbursement form that calculates a miles field in each row of a table. We have had the request to have the ability to mark a row as roundtrip. I like the idea, but I'm not sure how to implement it. Basically if the roundtrip checkbox is selected, it would need to grab the value of the miles field for that row and multiply it by 2 and replace the miles value. Below is the JavaScript we currently have (thanks to Answers).

 

//Start Add row mile fields together
    $('.cf-table-block').on('blur', 'input', sumtotal);
    if ($('.subtotal').length > 0) {
    $('.cf-table-block').on('blur', 'input', rowtotal);
    }
    function sumtotal() {
    var sum = 0;
    $('td.sum').each(function () {
    var s = 0;
    $(this).find('input').each(function () {
    s += parseNumber($(this).val());
    })
    $(this).find('.subtotal input').val(s);
    sum += s;
    });
    $('.total input').val(sum);
    }
    function rowtotal() {
    var sum = 0;
    $('.cf-table-block tbody tr').each(function () {
    var s = 0;
    $(this).find('.sum input').each(function () {
    s += parseNumber($(this).val());
    })
    $(this).find('.subtotal input').val(s);
    sum += s;
    });
    }
    function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
  //End Add row mile fields together

 

0 0

Answer

APPROVED ANSWER SELECTED ANSWER
replied on March 19, 2014

Assuming there is a checkbox field that has one option ('Yes') and has the roundTripCheck class, this should do it:

 

$(document).ready(function () {
    $('.cf-table-block').change(sumtotal);

    function sumtotal() {
        var sum = 0;


        $('td.sum').each(function () {
            var s = 0;


            $(this).find('input').each(function () {
                if ($(this).closest('td').siblings('td.roundTripCheck').find('input').is(':checked')) {
                    s += parseNumber($(this).val() * 2)
                }
                else {
                    s += parseNumber($(this).val());
                }
            })

            $(this).find('.subtotal input').val(s);
            sum += s;
        });
        $('.total input').val((sum).toFixed(2));
    }

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

});

I removed some of the code for calculating a row subtotal, since that seems unnecessary for this example.

0 0
replied on March 19, 2014

Works great! Thank you again Eric. You have helped us so much!

0 0
replied on March 19, 2014

You're welcome!

0 0
replied on April 22, 2014

For some reason when I use this script the total field does not maintain integrity when i click on anything else, such as submit. What am I doing wrong?

Replies

replied on March 19, 2014

I am sorry, but I am slightly confused about the output of the provided code. are you storing the amount for each row in a field that contains that information in the table? 

 

Roundtrip would just be looking for a value from a checkbox field and if it is checked, then to set a variable to 2. if the checkbox is not checked, then the variable you use should stay as '1'. this way you can add it into your calculations and it won't effect the calculation to have that variable in there when it's not checked.

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

Sign in to reply to this post.