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

Question

Question

Forms Calculation Help

asked on February 23, 2016 Show version history

Hi,

I have a bit of complicated requirement, I am not sure if this can be achieved by using Forms 10 built in calculations or has to be done via Scripting?

Please see attached screenshots:

The field Beginn & Ende are configured as Time fields.

Pause has values 0.25, 0.50, 1.00 & so on, which in relation to this form is meant to be break time.

Pax is number of people

Funktion, is not relevant for the question.

Stunden is meant to be a calculation, this is meant to be:

Time from Beginn to Ende minus Pause, multiplied by Pax = Stunden

 

Another thing is NZ* Std is to be calculated any no. of hours where Beginn is after 23:00 this means, that if the work starts at 17:00 & goes till 4:00, Stunden will be 11 hours & NZ* Stunden will be 5 hours.

 

I would really appreciate if anyone could help me either guiding if it's possible with formulas in Forms or with Javascript?

Thanks in advance!

S

Screen Shot 2016-02-23 at 15.17.01.png
Screen Shot 2016-02-23 at 15.17.25.png
0 0

Answer

SELECTED ANSWER
replied on February 25, 2016 Show version history

Hi Sahil,

I'd recommend using external JavaScript like moment.js for this. For more information, click here and scroll down to the "Including external JavaScript and CSS files" section. 

Then you can use the following code:

$(document).ready(function(){
  $.getScript('http://FormsServer/Forms/js/moment.js');
  $('.cf-table-block').on('change', function(){
    $('.cf-table-block tbody tr').each(function(){
      var beginn = moment($(this).find('.beginn select').val(), 'HHmm');
      var ende = moment($(this).find('.ende select').val(), 'HHmm');
      var pause = parseNumber($(this).find('.pause select').val());
      var pax = parseNumber($(this).find('.pax select').val());
      var $stunden = $(this).find('.stunden input');
      var $nz = $(this).find('.nz input');
      
      var hours = ende.diff(beginn, 'hours', true) - pause;
      var over = ende.diff(moment('0', 'HHmm'), 'hours', true);
      
      if(hours < 0){hours += 24; $nz.val((over + 1).toFixed(2));}
      else{
        if(over > 23){$nz.val((over - 23).toFixed(2))}
        else{$nz.val((0).toFixed(2));}
      }
      
      $stunden.val((hours*pax).toFixed(2));
    });
  });
  function parseNumber(n){ 
    var f = parseFloat(n);
    return isNaN(f)?0:f;
  } 
});

Make sure to add classes to your column fields (i.e. beginn, ende, nz...), and to change FormsServer on line 2 to your Forms server name.

Let me know if this works for you!

0 0

Replies

replied on February 26, 2016

Alex,

Awesome, Beautiful, WOW, Respect!!!!

 

You are the Man!!!

 

 

0 0
replied on February 26, 2016

Alex,

Just one thing, when the NZ* Std is filled, it gives an error to fill an integer?

0 0
replied on February 26, 2016 Show version history

Alex,

I wanted to take it a step further in terms of calculations, Please see the code attached:

$(document).ready(function(){
  $.getScript('http://localhost/Forms/js/moment.js');
  $('.cf-table-block').on('change', function(){
    $('.cf-table-block tbody tr').each(function(){
      var beginn = moment($(this).find('.beginn select').val(), 'HHmm');
      var ende = moment($(this).find('.ende select').val(), 'HHmm');
      var pause = parseNumber($(this).find('.pause select').val());
      var pax = parseNumber($(this).find('.pax select').val());
      var $stunden = $(this).find('.stunden input');
      var $nz = $(this).find('.nz input');
      var $price1 = $(this).find('.price1 input'); //NEW
      var $price = $(this).find('.price input'); //NEW
            
      
      var hours = ende.diff(beginn, 'hours', true) - pause;
      var over = ende.diff(moment('0', 'HHmm'), 'hours', true);
      var nzhours = ende.diff(beginn, 'hours', true);
     
          if(hours < 0){hours += 24; $nz.val((over + 1).toFixed(2));}
      		else{
          		if(over > 23){$nz.val((over - 23).toFixed(2))}
        		else{$nz.val((0).toFixed(2));}
      }
      
      $stunden.val((hours).toFixed(2));
     $price.val((pax *$stunden)*$price1); //NEW
  
    });
  });
  
   
    function parseNumber(n){ 
    var f = parseFloat(n);
    return isNaN(f)?0:f;
  } 
});

I am trying to make the calculation generating result on field price.

it has to be multiple of pax & stunden, the resulting number should be multiplied with price 1, resulting in the value in price.

 

When I use the code above , I get NaN in price?

 

0 0
replied on February 26, 2016

I have only modified lines, 11, 12 & 26

 

Thanks a lot in advance!

 

0 0
replied on February 26, 2016 Show version history

Hi Sahil,

Each variable that starts with "$" is just the selector, not the value (they do not have ".val()" in the declaration). Therefore, $price1 (line 11) should be:

var price1 = parseNumber($(this).find('.price1 input').val());

And line 26 should be:

$price.val(((pax*$stunden.val())*price1).toFixed(2));

 

0 0
replied on February 26, 2016 Show version history

Alex,

Thanks, what I don't understand is if line 11 is to be the way you mentioned, then why not line 12?

My thinking is in line 11 I gave $ because it's a CSS class, is that correct?

Another thing

  • NZ* Std still gives the error , Please enter an integer
  • & the calculations do not follow if I add rows in the table.

 

Thanks a lot in advance!

 

0 0
replied on February 26, 2016

The "$" before the variable name does not mean anything, it is just a handy way to see which variables are selecting elements and not their values. The ones without "$" all have ".val()" at the end.

The second line I gave you is wrong: $price1.val() should be price1

0 0
replied on February 26, 2016 Show version history

NICE!!!

Works!!

Only 1 issue now,

when I move away from the fields it gives the error Please enter an integer, only happens when I move the cursor away from it, I skip then no error....

If I make it read only , then it's fine, but that's not what I want to do... please suggest

0 0
replied on February 26, 2016

Hi Sahil,

Which fields give you the error?

0 0
replied on February 26, 2016

NZ* Std with Css class = nz

Betrag CHF with class = price

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

Sign in to reply to this post.