0

I'm looking to correlate two fields of my form, using the same index number.

I mean, I have a group of fields called traj in an array.
Each traj field is (or should be) related to a niv field, with the same index number (hierarchical index).
I would like to find a specific word in one of the traj fields, get the number on the related niv field and modify the result field with it.
If there isn't the word, nothing happens.

My code is:

// there are 6 traj fields (traj.0, and so on, up to traj.5) 
// and 6 niv fields (same way, niv.0 to niv.5)
// , and a result field

var oAtzar = this.getField("traj").getArray(); // create the array

for (i = 0; i < oAtzar.length; i++) {    // begin the loop
  var tNom = oAtzar[i].valueAsString     // define I'm looking for a word inside the array

  if (tNom === "Jesuïta") {              // if the word is the one I'm looking for...
    var tNiv = this.getField("niv." + i) // check the niv field related to the traj field; same *i* value
    event.value = 16 - 1 * (tNiv.value)  // modify the result field (autocalculated)
  } else {
    event.value = 16                     // no word, nothing happens
  }
}

However, I found some issues:

  • It doesn't modify the result field. I though as the i value is the same, it would work, but is doesn't.
  • Checking before publishing I found that it finds the word, but only in the first field. If I write it in any other field of the array, it doesn't find anything.

I know I can do it with a cascade of if...else, but I though it would be faster (and easier) with an array and a loop.
I hope I have detailed well enough!
Thank you in advance for your help!

EDIT: Here I add the file (.pdf)

2
  • 5
    Please include a minimal reproducible example! Commented Sep 12 at 12:07
  • It seems likely that you forgot to break the loop after you found the word you searched for. This is a typo-like error. This is not essential, but it also appears that the code under else has to be performed only if the word is not found in any element of the array, and not each time an element is a mismatch. So, the logic of the code might be something like the javascript in this fiddle. Commented Sep 12 at 12:39

1 Answer 1

2

As @kikon pointed out for a single unique keyword this is down to a decision when to break a programming loop.

Here we would see the added break position and the correct result.

enter image description here

    event.value = 16 - 1 * (tNiv.value);
    break; // *** IMPORTANT STOP TESTING ON MATCH ***
  } else {

However we now have the MRE and see the aim is to reduce from 16 Max based on ANY match subtracted from that and thus the task is different! Thus to acheive that we need to refactor a bit !

enter image description here

var oAtzar = this.getField("traj").getArray();  //initialise
var t = 16;  //set a max value
for (i = 0; i < oAtzar.length; i++) {
  if (oAtzar[i].valueAsString === "Jesuïta") {
    t -= (this.getField("niv." + i).value); //subtract every match
  }
}
event.value = t < 0 ? 0 : t; //ensure value does not go negative

However as commented by the Original Poster that does not work as illustrated here where the left field and right fields are not following a similar expected order. So the editor is not using same sequencing.

![enter image description here

Currently I am trying to understand what may be needed apart from rebuild the fields in a different order, which works when I try that!

MRE FIELDS

14 0 obj
<</Kids[24 0 R 25 0 R 26 0 R 27 0 R 28 0 R 29 0 R]/T(niv)>>
endobj
15 0 obj
<</Kids[8 0 R]/T(salva)>>
endobj
16 0 obj
<</Kids[30 0 R 31 0 R 32 0 R 33 0 R 34 0 R 35 0 R]/T(traj)>>
endobj

EXPECTED TAB order 0-5 and result but does not have to be 
18 0 obj
<</Type/Page/Annots[
30 0 R 24 0 R 
31 0 R 25 0 R 
32 0 R 26 0 R 
33 0 R 27 0 R 
34 0 R 28 0 R 
35 0 R 29 0 R
 8 0 R]

OBSERVED MRE file order

18 0 obj
<</Type/Page/Annots[
35 0 R 24 0 R = 35 0 obj <</T(0)/V(Jesu\357ta)>> endobj 24 0 obj <</T(3)/V()>> endobj
34 0 R 25 0 R = 34 0 obj <</T(1)/V(Jesu\357ta)>> endobj 25 0 obj <</T(4)/V()>> endobj
33 0 R 26 0 R = 33 0 obj <</T(2)/V(Jesu\357ta)>> endobj 26 0 obj <</T(5)/V()>> endobj
32 0 R 27 0 R = 32 0 obj <</T(3)/V(Jesu\357ta)>> endobj 27 0 obj <</T(0)/V()>> endobj
31 0 R 28 0 R = 31 0 obj <</T(4)>> endobj               28 0 obj <</T(1)/V()>> endobj
30 0 R 29 0 R = 30 0 obj <</T(5)>> endobj               29 0 obj <</T(2)/V()>> endobj
 8 0 R]

So the order seems to end up reverse to expectation enter image description here

The supplied MRE.PDF works with above code if the fields are "dragged" to these locations. HOWEVER the tab order is very odd so best rebuild.

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you everyone for your help. I have added a file with the fields as you asked (I never thought it was necessary, so sorry). However, it doesn't work as I expected. I wanted to find the word in any of the fields, eve if the others are empty, and now it only modifes the result when some fields are filled. It is the class section of an RPG character sheet where the pc can have from one to six classes (but usually will have only one). Maybe the main issue is the planning of the code itself. I'll give another think to it....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.