Page 1 of 2

onchange event

Posted: Wed Nov 09, 2016 1:05 pm
by ppbedz
Can I force the onchange event to be activated for a field on the screen? My scenario is as follows: If a user clicks on a hyperlink, it causes a screen field value to be reset to it's default. I have an onchange event on that same screen field to cause calcualations to be performed. The onchange event works fine when the user manually changes the value in the field. It does not execute the onchange event when the javascript code from the hyperlink causes the field value to change.

This is what happens now:

link clicked, field2 changed to 7 by javascript, no calculations take place
link not clicked, field2 changed to 7 by the user, calculations take place

I need the calculations to take place either way.

Thank you,
Patti

Re: onchange event

Posted: Wed Nov 09, 2016 2:35 pm
by Scott Klement
Put the code in your onchange event into a function. Have onchange call that function. Have something else (whatever is the other condition... clicking a hyperlink?) call the function as well.

Re: onchange event

Posted: Wed Nov 09, 2016 3:17 pm
by ppbedz
Scott,

I knew I would eventually have to create a function because I have the same javascript on a couple different fields. The problem is that the javascript re-calculates multiple fields from a grid row and then updates the row. (See "javascript calculations" entry from 11/9/16 in forum). I thought a function could only return one value. Not sure how to do all that in one function. Patti

Re: onchange event

Posted: Wed Nov 09, 2016 3:28 pm
by Scott Klement
Patti,

The "return" value of a JS function can only have one value, but that value can be an object (the JS equivalent of RPG's data structure) so in that respect it can return as many values as you want.

I'm not understanding how this relates to your example from the "javascript calculations" thread, as that example doesn't return anything at all.

Re: onchange event

Posted: Wed Nov 09, 2016 3:43 pm
by ppbedz
That's why I did not know how I could turn it into a function. That code is currently on my "onchange" event for my hours worked and my cost/hour fields. There is a hyperlink that resets the cost/hour to a default value if the user clicks. However, the since the user did not enter a value, the "onchange" for the cost/hr is not executed. I don't want to have duplicate javascript code on the 2 fields and I would like to have the hyperlink execute the javascript code as well. Hopefully, that gives a better picture of what I am trying to accomplish....

Re: onchange event

Posted: Wed Nov 09, 2016 3:58 pm
by Scott Klement
I'm not having any trouble understanding your code. What I don't understand is where you're stuck? Just turn it into a function and call it from both places...

Code: Select all

function pattisFunction() {

  var hrrate = pui.get("T_CstHr.1");	
  var hrswrk = pui.get("T_HrsWrkd.1");
  var csmcst = pui.get("T_CsmCst.1");
  var msccst = pui.get("T_MscCst.1");
  var hidcst = pui.get("T_HidCstHr.1");

  if (hrrate !== 0 && hrswrk !== 0 && hrrate !== "" && hrswrk !== "")
  {
    var labcstc = (hrswrk * hrrate);  
    var labcst = labcstc.toFixed(2);
    var totcstc = (labcst + csmcst + msccst);
    var totcst = totcstc.toFixed(2);
    pui.set("T_LabCst.1",labcst);
    pui.set("T_TotCst.1",totcst);
   getObj("CostGrid").grid.refresh();
  }
}
Set onchange to pattisFunction(). When the hyperlink is clicked, also run pattisFunction(). I'm not understanding what this has to do with return values, etc.

Re: onchange event

Posted: Wed Nov 09, 2016 4:10 pm
by ppbedz
Scott, I think maybe I was not understanding. I did not realize I could execute a function to do a task for a particular screen (similar to a subroutine in an rpg program). I will try it. Thanks. - Patti

Re: onchange event

Posted: Thu Nov 10, 2016 9:20 am
by ppbedz
Scott,

I set up my function and started out by running from the hyperlink. When it attempts to execute it tells me "On Click Error updReqCost is undefined"


This is the code on the onclick event:

var hidcst = pui.get("T_HidCstHr.1");

pui.set("T_CstHr.1",hidcst);

applyProperty("T_OvrMsg", "visibility", "hidden");
applyProperty("T_OvrLnk", "visibility", "hidden");

updReqCost();


This is the function:

function updReqCost() {

var hrrate = Number(pui.get("T_CstHr.1"));
var hrswrk = Number(pui.get("T_HrsWrkd.1"));
var csmcst = Number(pui.get("T_CsmCst.1"));
var msccst = Number(pui.get("T_MscCst.1"));
var hidcst = Number(pui.get("T_HidCstHr.1"));


if (hrrate !== 0 && hrswrk !== 0 && hrrate !== "" && hrswrk !== "")
{
var labcstc = Number(hrswrk * hrrate);
alert("calc lab= " + labcstc);
var labcst = labcstc.toFixed(2);
var totcstc = Number(labcst + csmcst + msccst);
var totcst = totcstc.toFixed(2);
pui.set("T_LabCst.1",labcst);
pui.set("T_TotCst.1",totcst);
getObj("CostGrid").grid.refresh();


if (hrrate != hidcst)
{
applyProperty("T_OvrMsg", "visibility", "visible");
applyProperty("T_OvrLnk", "visibility", "visible");
}
else
{
applyProperty("T_OvrMsg", "visibility", "hidden");
applyProperty("T_OvrLnk", "visibility", "hidden");
}

}
}

}

Re: onchange event

Posted: Thu Nov 10, 2016 10:35 am
by Glenn
Patti,

Where did you put the updReqCost() function?

Glenn

Re: onchange event

Posted: Thu Nov 10, 2016 10:38 am
by ppbedz
Hi Glenn,

Z:\profoundui\htdocs\profoundui\userdata\custom\js\engwkrq.js

The function is defined in the file in engwkrq.js

We have other functions in Z:\profoundui\htdocs\profoundui\userdata\custom\js that are working.

Patti