Page 1 of 2

Javascript calculations

Posted: Wed Nov 09, 2016 9:21 am
by ppbedz
Hi,

I have a one row grid that contains hrs worked, cost/hr, and some other costing components. The intent of my Javascript is that when either hrs worked or cost/hr changes, the labor cost should be recalculated as well as the final total.
I am having an issue trying to calculate labor cost to a rounded 2 decimal dollar value. The "toFixed(2) works if I have decimals in the result, but if the result equates to a whole number, I get a popup error ("Object doesn't support property or method "toFixed"" ) I also get a very strange result when I try to add my 3 cost components together and post the total to my grid. I could use some help with my javascript. Thank you...

Re: Javascript calculations

Posted: Wed Nov 09, 2016 10:11 am
by emhill
Just a thought and this was given to me by our former onsite JavaScript expert. Try using the parseFloat on your var definitions. Something like this:

var finhours = parseFloat(get("ZATHRS")) || 0;

I have no idea if this will help you but was just something I remembered we did.

I miss my onsite JavaScript expert. :-(

Re: Javascript calculations

Posted: Wed Nov 09, 2016 11:26 am
by Glenn
I just threw together a quick test and it seems to work fine for me. Could you debug the script and see what the value of 'totcstc' is before the toFixed() method call?

Glenn

Re: Javascript calculations

Posted: Wed Nov 09, 2016 11:53 am
by ppbedz
Glenn,

I am getting the error on the statement below. Mine does not get to the calculation for totcstc. My fields are all defined as 9,2
Also, it seems I get the error no matter how many decimal positions are in my result.

var labcst = labcstc.toFixed(2);


Test 1
hrs = 2
cost/hr = 50
labcst = 100
then I get the error message. It does not get to the calculation for totcstc.

Test 2
hrs = 2
cost/hr = 50.25
laccst = 100.5
then I get the error message. It does not get to the calculation for totcstc.

Test 3
hrs = 1
cost/hr = 50.75
labcst = 50.75
then I get the error message. It does not get to the calculation for totcstc.

Re: Javascript calculations

Posted: Wed Nov 09, 2016 11:57 am
by Glenn
What's the value in "labcstc" when the error message occurs?

Glenn

Re: Javascript calculations

Posted: Wed Nov 09, 2016 12:01 pm
by ppbedz
I'm sorry, "labcstc" is the field from my tests, not "labcst". "labcstc" seems to be calculated properly. The "to.Fixed" errors when I try to move to "labcst" . I have a similar issue with the code that calculates "totcstc" and uses "to.Fixed" to move to "totcst".

Patti

Re: Javascript calculations

Posted: Wed Nov 09, 2016 12:16 pm
by Glenn
I'm not certain if this is the issue, but the toFixed() method only works on numbers. I would suggest that you try explicitly converting your data to numbers before doing any calculations. I would try wrapping your get() API calls like this:

Code: Select all

var hrrate = Number(pui.get("T_CstHr.1"));	
Glenn

Re: Javascript calculations

Posted: Wed Nov 09, 2016 12:35 pm
by ppbedz
Much Better! Thank you!

Re: Javascript calculations

Posted: Wed Nov 09, 2016 12:39 pm
by ppbedz
Glenn,

I am confused as to when I need to use "grid.refresh" and/or "grid.render". Can you explain?

Thank you,
Patti

Re: Javascript calculations

Posted: Wed Nov 09, 2016 2:53 pm
by Glenn
Patti,

Below are links and a short explanation to each API you mentioned.

grid.refresh() - Use this API to reload the data in the grid if it's loaded via 'dynamic data' (ex: database or data url driven grids)
http://www.profoundlogic.com/docs/pages ... Id=5832751

grid.render() - Use this API to re-render the cells of the grid after making 'look and feel' changes to the cells (ex: changing column widths)
http://www.profoundlogic.com/docs/pages ... Id=9076747

Glenn