Javascript calculations

Use this board to ask questions or have discussions with other Rich Displays users.
ppbedz
Experienced User
Posts: 147
Joined: Tue Jun 17, 2014 4:00 pm
First Name: Patti
Last Name: Bednarz
Company Name: McGard
State / Province: New York
Country: United States
Contact:

Javascript calculations

Post 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...
Attachments
javacostcode.docx
(92.15 KiB) Downloaded 217 times
emhill
Experienced User
Posts: 116
Joined: Wed Sep 05, 2012 11:14 am
First Name: Eric
Last Name: Hill
Company Name: Integrated Corporate Solutions
Phone: 256-760-8239
Address 1: 501 S Wood Avenue
City: Florence
State / Province: Alabama
Zip / Postal Code: 35630
Country: United States
Contact:

Re: Javascript calculations

Post 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. :-(
User avatar
Glenn
Profound Logic Staff Member
Posts: 124
Joined: Mon Apr 14, 2014 4:08 pm
First Name: Glenn
Last Name: Hopwood
Company Name: Profound Logic Software
State / Province: Ohio
Country: United States
Contact:

Re: Javascript calculations

Post 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
ppbedz
Experienced User
Posts: 147
Joined: Tue Jun 17, 2014 4:00 pm
First Name: Patti
Last Name: Bednarz
Company Name: McGard
State / Province: New York
Country: United States
Contact:

Re: Javascript calculations

Post 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.
User avatar
Glenn
Profound Logic Staff Member
Posts: 124
Joined: Mon Apr 14, 2014 4:08 pm
First Name: Glenn
Last Name: Hopwood
Company Name: Profound Logic Software
State / Province: Ohio
Country: United States
Contact:

Re: Javascript calculations

Post by Glenn »

What's the value in "labcstc" when the error message occurs?

Glenn
ppbedz
Experienced User
Posts: 147
Joined: Tue Jun 17, 2014 4:00 pm
First Name: Patti
Last Name: Bednarz
Company Name: McGard
State / Province: New York
Country: United States
Contact:

Re: Javascript calculations

Post 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
User avatar
Glenn
Profound Logic Staff Member
Posts: 124
Joined: Mon Apr 14, 2014 4:08 pm
First Name: Glenn
Last Name: Hopwood
Company Name: Profound Logic Software
State / Province: Ohio
Country: United States
Contact:

Re: Javascript calculations

Post 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
ppbedz
Experienced User
Posts: 147
Joined: Tue Jun 17, 2014 4:00 pm
First Name: Patti
Last Name: Bednarz
Company Name: McGard
State / Province: New York
Country: United States
Contact:

Re: Javascript calculations

Post by ppbedz »

Much Better! Thank you!
ppbedz
Experienced User
Posts: 147
Joined: Tue Jun 17, 2014 4:00 pm
First Name: Patti
Last Name: Bednarz
Company Name: McGard
State / Province: New York
Country: United States
Contact:

Re: Javascript calculations

Post 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
User avatar
Glenn
Profound Logic Staff Member
Posts: 124
Joined: Mon Apr 14, 2014 4:08 pm
First Name: Glenn
Last Name: Hopwood
Company Name: Profound Logic Software
State / Province: Ohio
Country: United States
Contact:

Re: Javascript calculations

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests