Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Where to put a function code that changes visibility of multiple objects based on the value of multiple objects

Avatar

Level 1

I have a function that I was assisted by AI to write which changes the visibility of a bunch of objects based on the value of a bunch of dropdowns and checkboxes. It also includes listener events so that when these dropdowns and checkboxes are changed the visibility of the items affected by them is updated as well. My problem is that I do not know JavaScript very well and the folly of AI is that it's not always good at answering what I'm asking, but neither is my searching of Google so it's probably just me trying to do JavaScript things without a solid understanding of JavaScript that's causing the issue. 

Where does the function go?

Does it go into the master pages Initialize? Does it go under every dropdown and check boxes Change?  The code is over 500 lines, and that seems like it would bloat the document quite a lot and be very redundant.

What about the listener events? The AI tells me to put the listener events into the master pages' Initialize as well as every objects' Change, which seems like quite a lot, and doesn't feel right, but I don't know enough to be able to find the answers myself.

Here's a small portion to give you an example of the code:

xfa.resolveNode("ACQTypedrop").addEventListener("change", handleFieldChange);
xfa.resolveNode("KTrevdrop").addEventListener("change", handleFieldChange);
xfa.resolveNode("KTdollarfill").addEventListener("change", handleFieldChange);

function handleFieldChange(event) {
  var acqType = xfa.resolveNode("ACQTypedrop").rawValue;
  var ktRev = xfa.resolveNode("KTrevdrop").rawValue;
  var ktDollar = xfa.resolveNode("KTdollarfill").rawValue;
  
if ((acqType == "1" || acqType == "2" || acqType == "3") && ktRev == "1") {
xfa.resolveNode("S1Q1").presence = "visible";
xfa.resolveNode("S1Q3").presence = "visible";
} else {
xfa.resolveNode("S1Q1").presence = "hidden";
xfa.resolveNode("S1Q3").presence = "hidden";
}

if ((acqType == "1" || acqType == "2" || acqType == "3") && ktRev == "1" && ktDollarFill >= 250000) {
xfa.resolveNode("S2Q1").presence = "visible";
} else {
xfa.resolveNode("S2Q1").presence = "hidden";
}
}

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 3

HartHome,

 

I do not believe that .addEventListener is a method or property in Adobe XFA Javascript. If you put that code in and run it in Acrobat with the JavaScript Debugger it will throw an error when it tries to run those lines of code.

 

If you want to control visibility think about putting your code on the calculate event of a container. Subforms are containers that allow for code to be used in a calculate event.

 

The way your code is written it will look at the field values and then apply the conditional logic to work on the .presence. You would not need the addEventListener lines of code.

View solution in original post

3 Replies

Avatar

Community Advisor

If you want to change the data on the events of different fields and then put it on the respective event of that field, eg. change or exit event of the dropdown.

 

Another option is, to take a button on the form and call your main function on the click event of that button and check the different field values used in your business logic.

Avatar

Correct answer by
Level 3

HartHome,

 

I do not believe that .addEventListener is a method or property in Adobe XFA Javascript. If you put that code in and run it in Acrobat with the JavaScript Debugger it will throw an error when it tries to run those lines of code.

 

If you want to control visibility think about putting your code on the calculate event of a container. Subforms are containers that allow for code to be used in a calculate event.

 

The way your code is written it will look at the field values and then apply the conditional logic to work on the .presence. You would not need the addEventListener lines of code.

Avatar

Administrator

@HartHome Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni