Copy link to clipboard
Copied
I have a project that creates pages from a template. Some of my fields are duplicated and not renamed, so I have multiple widgets of the same fields. I notice that when I delete pages, the widgets numbers adjust instead of remaining as they were. For example, the field 'NBI' is replicated. There are two instances on each page, and the template hosts #0 and #1. So my first page has #2 and #3, second page has #4 and #5, etc. If I delete page 1, the widgets on page 2 (which becomes page 1) become #2 and #3 instead of remaining #4 and #5.
Does this always reliably happen?
Furthermore, if I move a page around wih the thumbnails pane, the widgets do not renumber. This isn't expected from the users, but it's obviously possible...
I have a check box that hides the bottom photo, caption, and info. The photo and caption are unique fields, so no trouble there. I need to be able to also hide the info fields. If I know for a fact that the number system reliably works as I described it above, I can hide widgets numbered (page number*2 + 1). The problem is if the pages are moved, or if the system doesn't always number this way.
It does seem that the top fields are always numbered first sequentially, then the bottom fields. Is there a way to get a widget with the higher number on a page? I guess I could even go off the position the widget is in.
Any advice/direction would be appreciated.
Yes, this is correct and expected behavior. The widget numbers always start from zero and move on in sequential order. If you remove elements from the middle of the array the other elements are pushed back and take their index numbers.
Also, the order in which the widgets are numbered has nothing to do with page number or physical location. It's the order in which they were created.
Try this code:
function returnHighestWidgetOnPage(doc, p, fieldName) {
var f = doc.getField(fieldName);
if (f==null) return;
var page = f.page;
if (typeof page=="number") {
if (page==p) return 0;
} else {
var idx = page.lastIndexOf(p);
if (idx!=-1) return idx;
}
return;
}
And you call it like this:
returnHighestWidgetOnPage(this, 1, "Text3")
It will return either the highest index number of the widget for that field name on that page, or null.
Copy link to clipboard
Copied
Yes, this is correct and expected behavior. The widget numbers always start from zero and move on in sequential order. If you remove elements from the middle of the array the other elements are pushed back and take their index numbers.
Also, the order in which the widgets are numbered has nothing to do with page number or physical location. It's the order in which they were created.
Copy link to clipboard
Copied
Awesome. Now how can I use the widget index in my script? What I need is a modified version of the following function that can return the widget with the highest index on a page:
function getFieldOn(name, p){//finds (the first) field on page p with 'name' in its name
var fields;
for(var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
if ((f.name.indexOf(name) > -1) && (f.page == p)) return f;
}
}
I need to replace the if statement to get any fields of a given name, then I can compare the indexes.
Copy link to clipboard
Copied
Info: for fields with several widgets you will get for the page property an array of page numbers.
Copy link to clipboard
Copied
Are you asking what widget on a specific page has the highest index number, or on what page is the widget with the highest widget number?
Copy link to clipboard
Copied
The first one. I want to pick a page, get all the widgets and indexes from said page, then choose the one with the higher index number to toggle hidden/visible.
Copy link to clipboard
Copied
For a specific field?
Copy link to clipboard
Copied
Right. For example: every page has 2 widgets of the field "county". I have a toggle checkbox to hide the bottom photo/caption/all info associated with the bottom half. I need to call a function from a page that will allow me to only hide/unhide the widget of "county" with the highest index number (as it is created second and will always have a higher index).
Copy link to clipboard
Copied
Try this code:
function returnHighestWidgetOnPage(doc, p, fieldName) {
var f = doc.getField(fieldName);
if (f==null) return;
var page = f.page;
if (typeof page=="number") {
if (page==p) return 0;
} else {
var idx = page.lastIndexOf(p);
if (idx!=-1) return idx;
}
return;
}
And you call it like this:
returnHighestWidgetOnPage(this, 1, "Text3")
It will return either the highest index number of the widget for that field name on that page, or null.
Copy link to clipboard
Copied
I love it, this is working perfectly. BUT I need help undertstanding it...
I don't understand how this is getting the index of the widgets. I'm mainly looking at lines 7 and 8, where we have more than one widget of the same field name on the page (if I'm understanding that correctly).
So first, we check if the field exists. Then we create 'page', which will be either a single number or an array of numbers? Then if it is only one number, we force out a return of '0'. What are lines 7 and 8 about exactly?
Copy link to clipboard
Copied
If page is not a number, ie it's an array, search it for the last instance of the page number we're interested in.
If a match was found return that index number. If no match is found (idx is -1) do nothing, and the function will return null.
Copy link to clipboard
Copied
But how is the return just the index number of the widget? When does it look at the widget name and get just the number off of it?
Copy link to clipboard
Copied
Oh wait. Tell me if this is right. If the field exists on multiple pages, each one will be an item in the array. As long as they are in order, the index of the array item will happen to match the index of the widget's name. So this is actually returning the index of the array that has all the widgets of that name in it. Is this correct?
Copy link to clipboard
Copied
It returns the index from the pages array, which matches the index number of the widget itself.