Skip to main content
JMFreeman
Inspiring
January 24, 2020
Answered

Widget Numbers

  • January 24, 2020
  • 1 reply
  • 1530 views

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.

 

 

This topic has been closed for replies.
Correct answer try67

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). 


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.

1 reply

try67
Community Expert
January 24, 2020

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.

JMFreeman
JMFreemanAuthor
Inspiring
January 27, 2020

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.

try67
Community Expert
January 28, 2020

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?


It returns the index from the pages array, which matches the index number of the widget itself.