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

Question

Question

Row Calculation with update button

asked on July 17, 2017 Show version history

Hi all,

I require some to please write a script for me. smiley

I do believe the whole thing will have to be one Javascript?

We are creating an order form that goes to a "Picker" in a store room.

The admin staff complete the "Order Amount" field that is taken over the phone from the customer

The form then goes to the Storeroom for Picking.

For an example of what they want is:

The customer wants 2 Boxes of Chicken Thigh but when it goes to the Picker he will weigh the boxes as they work off weight not amount. Instead of having a separate calculator we want to include the calculator in the Table.

To give you a bit of an idea I have attached an example of the form.

The picker needs to enter an amount/weight into the "Calc Field" then press the "update checkbox" for that row.

This should add the "Calc field" amount to the "Total" field

The "Calc Field" should then be automatically cleared and the Update Checkbox automatically set back to Unticked.

This allows the Picker to add another amount to the Calc Field to update the Running Total for that item.

If I say pretty please does that help wink

Regards

Shane

Calc Form Design.jpg
0 0

Answer

SELECTED ANSWER
replied on July 24, 2017

Hey,

 

I've tested and the below code will work. The only thing that will need to be changed will be to target the correct Table ID.

 

$(document).ready(function(){

  

  //Sets the change even on all Tables

$('.cf-table-block').on('blur', 'input', CalculateWeight);

function CalculateWeight(){

  

  // Define and set initial totalWeight value to 0

  var totalWeight = 0;

$('#q15 tbody tr').each(function() {

 

  //checks if input value is blank

  if($(this).find('.weight input').val() != '')

  {

  var w1 = 0;

    var w2 = 0;

    // Take the value in the "Weight" column and add it to the value

    // of the totalWeight variable

    w1 = $(this).find('.weight input').val();

    w2 = $(this).find('.totalweight input').val();

    w1 = parseFloat(w1);

    //Checks for null values and updates accordingly

    if( w2 != '')

    {

      w2 = parseFloat(w2);

      totalWeight += w1+w2;

    }

    else

    {

      w2 = 0;

      totalWeight += w1;

    }

    

     

    // Plug totalWeight value into "Total Weight" field

    $(this).find('.totalweight input').val(totalWeight);

    // Clear the value in the "Weight" column

    $(this).find('.weight input').val(''); 

  }

  //Testing Purposes

//console.log(totalWeight);

});

}

});

2 0

Replies

replied on July 18, 2017

Hello Shane,

I'm a little unclear on the process once the form gets to the picker.

Say a customer orders 2 boxes of chicken thighs, per your example above. I assume that each "box" will weigh the same? If so, why would the picker need to update the amount again? What does the "Total" field track--total cost or total weight?

Since your form uses item codes, I'm wondering if it might be more expedient to setup a lookup rule to populate a hidden field with the official unit cost of each item, to use in the "Total" calculation?

For example, the admin would fill out the form for an order of 5 boxes of chicken thighs. The lookup rule would populate a hidden field with '.6' since that's how many kg of chicken thighs come in a box in this example. The total field would automatically populate with "3 kg" or $price*3*.6, or whatever the appropriate calculation would be.

~Rob

0 0
replied on July 18, 2017

Hi Rob, thanks for the reply.

Total is the total weight. 

The picker opens each box and weighs the chicken as no box is the same. It happens on a lot of their products. The first box might have 22.85kg of chicken and the next might have 24.04kg and since they charge by the kg they have to be accurate and if in smaller bag inside the box they weight each separately. So it could be that they are adding 8.9kg, 6.78kg, 5.45kg etc.

Hope this makes sense.

Regards

Shane

0 0
replied on July 18, 2017

Hi Shane,

Thanks for clarifying, that helps a lot.

I think I have a solution that will work for your form, and it will save the poor picker a step!

I started by reproducing the table in your example, but I ended up not using the "Update Total" checkbox--it's just a vestige in my screenshots below.

First, you need to add the "weight" and "totalweight" CSS classes to you table columns:

You can add classes like so:

 

Use this code to make the form work:

$(document).ready(function() {  
  // Define and set initial totalWeight value to 0
  var totalWeight = 0;
  
  // Trigger calculation on focusout (ie, tabbing out of field)
  $('.weight').focusout(function() {
    
    // Take the value in the "Weight" column and add it to the value
    // of the totalWeight variable
    totalWeight += parseFloat($('.weight input').val());
    
    // Plug totalWeight value into "Total Weight" field
    $('.totalweight input').val(totalWeight.toFixed(2));
    
    // Clear the value in the "Weight" column
    $('.weight input').val('');
  });
});

The way this works is, the picker will type the weight into the the "Weight" column field. When the picker tabs or clicks out of the "Weight" column field, the number they just typed is cleared from that field and added to whatever number is in the "Total Weight" column field. So, the picker would type the first value, tab to the next field or click anywhere outside the "Weight" field, then would tab or click back into the "Weight" field to enter the next value. This saves the hassle of having to click the "Update" checkbox every time, too.

~Rob

1 0
replied on July 18, 2017

That's awesome Rob, thank you, just one thing I found is if there are multiple rows and you add an amount to the first one it is adding it to Total weight in every row, is it possible to populate just that row? See attached

Thanks again for your help so far yes

Calc Form Row Pop.jpg
0 0
replied on July 19, 2017

Hi Shane,

Good catch--I think this should be fairly straightforward. I'll take another look at it today when I get a chance!

~Rob

0 0
SELECTED ANSWER
replied on July 24, 2017

Hey,

 

I've tested and the below code will work. The only thing that will need to be changed will be to target the correct Table ID.

 

$(document).ready(function(){

  

  //Sets the change even on all Tables

$('.cf-table-block').on('blur', 'input', CalculateWeight);

function CalculateWeight(){

  

  // Define and set initial totalWeight value to 0

  var totalWeight = 0;

$('#q15 tbody tr').each(function() {

 

  //checks if input value is blank

  if($(this).find('.weight input').val() != '')

  {

  var w1 = 0;

    var w2 = 0;

    // Take the value in the "Weight" column and add it to the value

    // of the totalWeight variable

    w1 = $(this).find('.weight input').val();

    w2 = $(this).find('.totalweight input').val();

    w1 = parseFloat(w1);

    //Checks for null values and updates accordingly

    if( w2 != '')

    {

      w2 = parseFloat(w2);

      totalWeight += w1+w2;

    }

    else

    {

      w2 = 0;

      totalWeight += w1;

    }

    

     

    // Plug totalWeight value into "Total Weight" field

    $(this).find('.totalweight input').val(totalWeight);

    // Clear the value in the "Weight" column

    $(this).find('.weight input').val(''); 

  }

  //Testing Purposes

//console.log(totalWeight);

});

}

});

2 0
replied on July 24, 2017

Awesome, Thanks Rob and Aaron for your help.

Works a charm Aaron !!!

Regards

Shane

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

Sign in to reply to this post.