Get field control from fields iterator

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

ericb
Ebase User
Posts: 82
Joined: Fri Jan 15, 2016 2:34 pm

Get field control from fields iterator

#1

Postby ericb » Tue Sep 06, 2016 7:20 pm

We're in a situation where we'd like to apply a CSS class to field controls based on whether the value of the associated field is different from an older value it had. The new and old values for each field are stored in a JSON object, which is iterated through to find which values differ. For these fields, we want the associated field control to have a CSS class.

The problem is that from the fields iterator or when using fields.getField() (img 1), there doesn't seem to be a way to call getFieldControl() like there is when accessing a field directly (img 2). I've tried adding .getFieldControl() after the getField() even though it doesn't get suggested, just to see if it would work, but I only get null.

Image

Image

I've tried going the opposite way, that is to do controls.getControl() and then call .getField() on the result, but that doesn't show up in the autocomplete either.

Is there no way to go from field to field control without directly accessing the field through fields.[fieldname]?
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#2

Postby Steve James » Wed Sep 07, 2016 8:36 am

Rough and ready but is this something like what you are trying to get at?

Code: Select all

var ary = pages.Page_1.getAllControls();

for each (var a in ary)
{
	if (a.elementType == 'Field Control' && a.elementName != 'ctrlRes')
	{
		var f = controls.getControl(a.elementName).getField();
		fields.res.value += f.elementName + ' [' + f.value + ']\r\n';
	}
}
Intellisense doesn't pop up but you can double check what's available via a standalone line of code.
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#3

Postby Jon » Wed Sep 07, 2016 8:44 am

You could do all controls on all pages with something like..

Code: Select all

for ( var control in Iterator(controls.iterator()) )
{
  if (control.elementType == "Field Control")
  {
  	var field = control.getField();
        ....
  }
}
The getFieldControl() method returns the first field control for the field on the current page. It will return null if the field doesn't appear on the current page or if there is no current page e.g. running a before form event.
0 x

ericb
Ebase User
Posts: 82
Joined: Fri Jan 15, 2016 2:34 pm

#4

Postby ericb » Wed Sep 07, 2016 1:17 pm

Steve: What is your code trying to do? it's not even 9 am here yet, so bear with me ;)

Jon: The script is running before form so that would explain why getFieldControl() returned null. I'll try the code you posted, thanks.
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#5

Postby Steve James » Wed Sep 07, 2016 6:04 pm

Hi Eric, I think I was trying to do a similar thing to Jon's example, that is get all controls, except I went for all controls on a page.

Once you have all the controls on a page you can loop through them, get at the field and set the class appropriately.

I couldn't see the images but sometimes intellisense doesn't indicate something is possible when in reality it is.

Hope you crack the problem.
Thanks
0 x

ericb
Ebase User
Posts: 82
Joined: Fri Jan 15, 2016 2:34 pm

#6

Postby ericb » Wed Sep 07, 2016 6:05 pm

Jon's code is indeed working so far, thanks to both of you for your time.
0 x


Who is online

Users browsing this forum: No registered users and 26 guests