Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Getting a Field Name With a Generic Number

Participant ,
Jan 15, 2020 Jan 15, 2020

For my project, I have pages spawn from a template. I use a hidden form field containing a count variable which I append to the names of spawned fields and increment. There are fields such as topPh#, topCap#, etc. These numbers may or may not match with page numbers or be in sequential order.

 

  • How can I make a loop that will go to each page and collect topPh# etc. regardless of the integer there? I also would want to have a list of the # values in order.

 

I used regular expressions to do something similar:

 

var num = event.target.name.match(/\d/g).join('');

this.getField("botPh" + num).display = (event.target.value=="Off") ? display.hidden : display.visible;

this.getField("botCap" + num).display = (event.target.value=="Off") ? display.hidden : display.visible;

 

But in the above case, my target widget has a number on it (e.g. togBot5), and it talks to other widgets with the same number (e.g. botPh5), regardless what the number is.

TOPICS
Acrobat SDK and JavaScript
876
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 15, 2020 Jan 15, 2020

Check the property page of the field.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 15, 2020 Jan 15, 2020

I'm sorry, it seems my intent is unclear.

 

Each page of my document has a top photo and a bottom photo. I'm working on buttons/features for the document to help manipulate the order of the photos. If I have 3 pages, so 6 photos total, and I want to delete photo 3 (which is the top photo of page 2), I plan to fill an array using buttonGetIcon for all the photos past photo 3. Then I can use buttonSetIcon and replace photo 3 with photo 4, replace 4 with 5, etc. (I'm going to worry about what happens to the last one later. It's much less disruptive to the user than deleting in the middle of the document.)

 

A rub to the problem is the names of the photo button fields. They are all named "topPh" or "botPh" with a unique number at the end. The number may not match the page number or be in sequence with the last number. I need to make a loop that will fill an array with the icons from each photo, starting at photo 3 and going to the end. In the following code, I need to figure out how to make the for loop find the fields, regardless of their number.

//pop up a confirmation window; yes = 4, no = 3
var confirm = app.alert("Are you sure you want to delete this photo?\nThis cannot be undone.", 1, 2, "Confirm Photo Deletion");

if(confirm == 4){       //this only runs if they say "yes", otherwise nothing happens.
     var pg = event.target.page;
     var aPg = this.numPages - pg;
     var icons = []
     for(var i = 0, i < aPg, i++){
          icons[i] = ???
     }     //end of for loop
}     //end of if statement

 The ??? is where I need to make it find the field "topPhX", for any value of X (as well as "botPhX"), then go the next page and find its fields, "topPhY" and "botPhY", where there is not necessarily a relationship between X and Y.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 15, 2020 Jan 15, 2020

I am thinking I need to make a function that I can pass a page number, and it figures out the field names on that page. Is there a way to use getField on a certain page maybe?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 15, 2020 Jan 15, 2020

Use getField and check the property page of the field.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 15, 2020 Jan 15, 2020

How can I use getField wihtout knowing the number that the field name will end with?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 15, 2020 Jan 15, 2020

Use a loop over all fields in the document.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 15, 2020 Jan 15, 2020

I can kind of see where to go with this, but I need a little more direction. Sorry... I'm definitely a beginner.

 

I came across a somewhat similar discussion which suggested the following:

function getPageFields(doc, p) {
var fields = [];
for (var i=0; i<doc.numFields; i++) {
var f = doc.getField(doc.getNthFieldName(i));
if ((typeof f.page=="number" && f.page==p) || (typeof f.page!="number" && f.page.indexOf(p)>-1)) fields.push(f.name);
}
return fields;
}

I don't understand the if statement though. If I mimic this approach, and have an array "fields" as in the example, how could I then keep only the ones on certain pages or with certain names?

 

Will looping through every field make the page run more slowly as the number of pages and fields grows? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 15, 2020 Jan 15, 2020

This function creates an array of all field names on page p.

 

When you have copies of fields with the same name the property page of the field gives an array.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 15, 2020 Jan 15, 2020

I'm getting closer. I need to modify the following:

     var fields = [];
     for (var i = 0; i < this.numFields; i++){
          var f = this.getField(this.getNthFieldName(i)).name;
          if(f == "topPh2") fields.push(f);
     }     //end of for loop
     app.alert(fields);

 Instead of

if (f == "topPh2") fields.push(f);

 I need something for any name beginning with "topPh". 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 15, 2020 Jan 15, 2020

Look like this works

     var fields = [];
     for (var i = 0; i < this.numFields; i++){
          var f = this.getField(this.getNthFieldName(i)).name;
          if(f.indexOf("topPh") > -1) fields.push(f);
     }     //end of for loop
     app.alert(fields);

I was avoiding indexOf. Is there an alternative? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 15, 2020 Jan 15, 2020
LATEST

You can also use the search method.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines