Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Most efficient way to loop through similarly named fields?

Avatar

Former Community Member


Hi,

I have a 5 page document with each page containing appx. 50 similarly named fields.    E.g. Viol1Num, Viol2Num, Vio3Num ...  Viol50Num.

I am looking for an efficient way of programming a loop to look at each field in Javascript so I can do some manipulations in those fields on what the user entered.

In FormCalc I've previously used the 'foreach' function similar to:

foreach (Field1, Field2, Field3.....Field50) do

     'BLAH'

endfor

however, that gets really lengthy, especially when dealing with subsequent pages where I have to start adding 'topmostSubform.Page2.' in front of each field name so that I can access from the first page all of the fields on subsequent pages.  Also, I need to do this in Javascript, not FormCalc.

For example, in JS I am using this loop to mark all fields as read only:







































    for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++) {
   var oFields = xfa.layout.pageContent(nPageCount, "field");
   var nNodesLength = oFields.length;
   for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {
   oFields.item(nNodeCount).access = "readOnly";
   }
   }

How could I do something similar to that so I could look through each field and perform actions on it without having to list out every single field name?

I tried altering that to look at fields instead of field properties, but I couldn't get it to run.

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

I have solved my issue.   It took some battling in javascript using xfa.resolveNode.

I have 5 pages, each consisting of a series of 60 fields named Viol1Num, Viol2Num, Viol3Num .... Viol60Num.

If when this javascript runs, it detects a blank field, then insert a '3' into it.

The below is the javascript which runs for the second page of this document.

   while (LoopCounter < 61) {
   if ((LoopCounter != 21) && (LoopCounter != 22)) {
   if((xfa.resolveNode("topmostSubform.Page2.Viol" + LoopCounter + "Num").rawValue == null) | (xfa.resolveNode("topmostSubform.Page2.Viol" + LoopCounter + "Num").rawValue == "")) {
   xfa.resolveNode("topmostSubform.Page2.Viol" + LoopCounter + "Num").rawValue = 3;
   }
   }
   LoopCounter = LoopCounter + 1
   }

View solution in original post

3 Replies

Avatar

Level 7

If you put your fields in subforms, you can reference the subform instead of the fields and perform operations on them.

Avatar

Former Community Member

Thanks for the reply, however I'm still at a loss on how to implement what you're describing.  My Concerns are:

-My document seems to have 1 topmostSubform, and then 6 pages under.   Adobe/LiveCycle structure on how things like this work is not my string suit.  Would I have to have a subform for each page?

-If referencing by subform, I don't want ALL fields on the page.  There are many other fields not applicable to what I was asking on my original post and I wouldn't want to modify them; only a series of similarly named fields.

Avatar

Correct answer by
Former Community Member

I have solved my issue.   It took some battling in javascript using xfa.resolveNode.

I have 5 pages, each consisting of a series of 60 fields named Viol1Num, Viol2Num, Viol3Num .... Viol60Num.

If when this javascript runs, it detects a blank field, then insert a '3' into it.

The below is the javascript which runs for the second page of this document.

   while (LoopCounter < 61) {
   if ((LoopCounter != 21) && (LoopCounter != 22)) {
   if((xfa.resolveNode("topmostSubform.Page2.Viol" + LoopCounter + "Num").rawValue == null) | (xfa.resolveNode("topmostSubform.Page2.Viol" + LoopCounter + "Num").rawValue == "")) {
   xfa.resolveNode("topmostSubform.Page2.Viol" + LoopCounter + "Num").rawValue = 3;
   }
   }
   LoopCounter = LoopCounter + 1
   }
The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----