Skip to main content
Participant
October 19, 2022
Question

Using Regular Expressions to determine layer visibility depending on dropdown selection

  • October 19, 2022
  • 1 reply
  • 1538 views

I am creating a non-dymamic PDF form that allows users to toggle layers based on several drop down selections. At frst this started as an individual call to each layer since I am a javascript noob and learning as I go, and now has evolvedto use RegEx to determine which layers should be filtered out for each function. I believe may layers are named in a workable convention, with parent layers named as whole numbers up to 12 (please ignore the RACH layers for now), with their children numbered as (whole number)"-"[1 - 10]. The first portion of this script is runnning smoothly, with the proper parent layers activating and deactivating, but it fails to catch the children layers. The first problem is that I cannot get fname to populate in the lower loops, even though it occurs during the same event. Another problem, I believe is my lack of knowledge on how to use a  variable inside of a Regular Expression, please see line, "var pcMaskMatch = /^[fname]/;". I know this is not written correctly, however I am having trouble finding resources that discuss this. The intent is to pull the fname from the ocgOnArray[] event, and compare it to the beginning of the filtered array. i.e. if Parent layer is called "10", I want to find all the "10-" layers. Any help on this is greatly appreciated, thank you all, and please see code below, which is scripted into the upper right dropdown (NumberOfTerminals) of the attached file:

 

(sidenote, action "on blur" is for potential use on ipad)

 

var pcMask = /^[\d]{1,2}$/;

//Tests for a digit in the first or second place, max string length

var drop = this.getField("NumberOfTerminals").value;

//Pulled from main drop-down

var ocgOnArray = this.getOCGs();

 

for (var i = 0; i < ocgOnArray.length; i++) {

var fname = ocgOnArray[i].name;

if (pcMask.test(fname) && ocgOnArray[i].name <= drop) {

//Testing to isolate parents then less or equal to drop-down value

ocgOnArray[i].state = true;

ocgOnArray[i].initState = true;

 

var gDropName = ("NumberOfTaps" + fname);

//Name of smaller drop-down located on each parent layer

var pcMaskG = /[-]/;

//All layers with dashes in name are children

var pcMaskMatch = /^[fname]/;

//Starts with tested parent layer name

var grilleArray = this.getOCGs();

 

for (var iG = 0; iG < grilleArray.length; iG++) {

var Gfname = grilleArray[iG].name;

if (pcMaskMatch.test(Gfname) && pcMaskG.test(Gfname) && grilleArray[iG].name <= this.getField("gDropName").value) {

//If it starts with the tested parent name, has a dash and less or equal

grilleArray[iG].state = true;

grilleArray[iG].initState = true;

}

if (pcMaskMatch.test(Gfname) && pcMaskG.test(Gfname) && grilleArray[iG].name > this.getField("gDropName").value) {

//More than

grilleArray[iG].state = false;

grilleArray[iG].initState = false;

}

}

if (pcMask.test(fname) && ocgOnArray[i].name > drop) {

//Wrapping up larger event, greater than

ocgOnArray[i].state = false;

ocgOnArray[i].initState = false;

}

}

}

This topic has been closed for replies.

1 reply

Isaac.HAuthor
Participant
October 31, 2022

Not that this thread has been active, but this was the solution:

 

//NumberOfTerminals local code
let drop = this.getField("NumberOfTerminals").value;

//Tests for 1 or 2 digits at the beginning of the string, then looks for the end of the string, children will fail this test
let parentMask = /^[\d]{1,2}$/;
//Tests for 1 or 2 digits at the beginning of the string, then looks for a dash, then 1 or 2 digits, then the end
let childMask = /^[\d]{1,2}-[\d]{1,2}$/;
//Tests for 1 or 2 digits at the beginning of the string, then possibly (allowed to fail) a dash and 1 or 2 digits, then the end
let tapsMask = /^\d{1,2}(-\d{1,2})?$/;
//Creates an array of all layer names
let layerList = this.getOCGs();

//this loop will cycle through each layer name and perform the following for each
for (let i = 0; i < layerList.length; i++) {
//creates a variable to hold the current layer name for testing
var fname = layerList[i].name;
//checks to see if it is a numbered layer, then if name is just a number, checks against main dropdown for greater than OR if the substring before the dash in the layer name is greater than the dropdown
if (tapsMask.test(fname) && (fname > drop || fname.substring(0, fname.indexOf("-")) > drop)) {
//if match, turn off layer
layerList[i].state = false;
layerList[i].initstate = false;
//If parent fails the previous, and is NOT greater than
} else if (parentMask.test(fname)) {
//Turn it on
layerList[i].state = true;
layerList[i].initstate = true;
//if it fails all previous and is a child layer, this works because all parent and children greater than the main are already off
} else if (childMask.test(fname)) {
//All the children whos group is equal or less than dropdown are going to pass what group they belong to
var currentTap = fname.substring(0, fname.indexOf("-"));
//Uses group name to grab the value of the associated dropdown, compares the substring after the dash. If the sub is greater than
if (fname.substring(fname.indexOf("-") + 1) > this.getField("NumberOfTaps" + currentTap).value) {
//Turn it off
layerList[i].state = false;
layerList[i].initstate = false;
//otherwise, turn on
} else {
layerList[i].state = true;
layerList[i].initstate = true;
}
}
}

try67
Community Expert
Community Expert
October 31, 2022

Be aware that initState can't be changed in Reader, and that none of this is likely to work on a mobile device.

Isaac.HAuthor
Participant
October 31, 2022

Fortunately on mobile, the file will be used on a different software platform that has this ability.