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

Question

Question

JavaScript Calculation in Forms not quite working

asked on August 23, 2016 Show version history

I have two tables in a timesheet, one is the timesheet daily entry the other is a separate table to record any hours used on machinery.

The Javascript calculation for the first timesheets works great, but when i try to use the same code for the machinery timesheet it fails.

 

$(document).ready(function () {
  
  
  
  $('.cf-table-block').on('blur', 'input', sumtotal);
  $(document).on('click', '.form-del-field', sumtotal);
  
  function sumtotal() {
    var hour = 0;
    var rate = 0;
    var total = 0;
    var subtotal = 0;   
    $('.cf-table-block tbody tr').each(function () {
      hour = moment($(this).find('.hour input').val(), 'HHmm');
      rate = moment($(this).find('.rate input').val(), 'HHmm');
      subtotal = hour.diff(rate, 'hours', true);
      $(this).find('.total input').val(subtotal.toFixed(2));
      total += subtotal;
    });  
    $('.Grandtotal input').val(total.toFixed(2));       
  }
  
  function parseNumber(n) { 
    var f = parseFloat(n); //Convert to float number. 
  	return isNaN(f) ? 0 : f; //treat invalid input as 0; 
  } 

});
 
// second jscript

 $(document).ready(function () {
   
   
    $('.cf-table-block').on('blur', 'input', sumtotal);
   $(document).on('click', '.form-del-field', sumtotal);
   
   function sumtotal() {
        var houra = 0;
        var ratea = 0;
        var totala = 0;
        var subtotala = 0; 

        $('.cf-table-block tbody tr').each(function () {
            houra =  parseNumber($(this).find('.houra input').val(), 'HHmm'); 
            ratea = parseNumber($(this).find('.ratea input').val(), 'HHmm');           
			subtotala = (houra - ratea); 

            $(this).find('.totala input').val(subtotala.toFixed(2));
            totala += subtotala;
        });

        $('.Grandtotal input').val(totala.toFixed(2));
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;

    }

});

The first Javascript is for the first table, the second javascript is the machinery table.

0 0

Answer

SELECTED ANSWER
replied on August 23, 2016

In second script, you can continue use moment instead of parseNumber as following:

           houra = moment($(this).find('.houra input').val(), 'HHmm');
           ratea= moment($(this).find('.ratea input').val(), 'HHmm');
           subtotala = houra.diff(ratea, 'hours', true);

 

0 0
replied on August 24, 2016

If i change the code to moment i get no results 

what does the "hours" do ? as its used in the first set of java-script too , does it need a different name ?

0 0
replied on August 24, 2016

The "hours" in the diff function is a parameter to tell that you want to check hours difference between houra and ratea, you don't need to change it.

It works for me when use two tables with two sets of JavaScript. Please double check the css class you configured for these fields and make sure they match the values in the JavaScript.

1 0
replied on August 24, 2016

Yes it does work , i had a mistake in my code , thank you for your help

0 0
replied on May 8, 2018

Are your start time, finish time and total hours fields Single Line, Number or Time fields?

0 0
replied on May 8, 2018

They are single line with a character limit of 4 

1 0

Replies

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

Sign in to reply to this post.