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;
}
}
}
