Search code examples
javascriptadobelivecyclelivecycle-designer

In Adobe Livecycle (possibly Javascript), how do I reference multiple pages/subpages to hide them simultaneously?


I suppose that this ultimately can apply to objects as well as pages. At the moment though, I'm really more concerned with pages and subforms.

I want to hide/show multiple pages based on a drop-down menu selection. I have of course searched regarding this subject. Nobody seems to be quite asking for what I'm asking, and the most closely related questions only answer for a single object rather than multiple. And I have something near 80 different pages/subforms to worry about.

I have a dropdown menu that, depending on the selection, certain pages and subforms which were previously hidden become visible. However, I am trying to build in a certain forgiveness, so that if someone accidentally selects the wrong item on the dropdown menu and goes back to correct themselves, either (1) all of the associated pages will go back to their default state of hidden, before continuing forward and making certain selections visible, or (2) each object is manually named and told to go back to being hidden.

The only way I've found to do this is as follows:

switch (newValue)
{
    case "1":
        this.resolveNode("subform_1").presence = "visible";
        this.resolveNode("subform_2").presence = "hidden";
        this.resolveNode("page_3").presence = "hidden";
        break;

    case "2":
        this.resolveNode("subform_1").presence = "hidden";
        this.resolveNode("subform_2").presence = "visible";
        this.resolveNode("page_3").presence = "hidden";
        break;

    case "3":
        this.resolveNode("subform_1").presence = "hidden";
        this.resolveNode("subform_2").presence = "hidden";
        this.resolveNode("page_3").presence = "visible";
        break;
}

This is tedious, time consuming, and inelegant. What I'd like to do instead is address multiple objects simultaneously. Something like:

switch (newValue)
{
    case "1":
        this.resolveNode("subform_2", "page_3").presence = "hidden";
        this.resolveNode("sabform_1").presence = "visible";
        break;

    case "2":
        this.resolveNode("subform_1", "page_3").presence = "hidden";
        this.resolveNode("subform_2").presence = "visible";
        break;

    case "3":
        this.resolveNode("subform_1", "subform_2").presence = "hidden";
        this.resolveNode("page_3").presence="visible";
        break;
}

I'm aware that this is seen as inelegant in and of itself, but given the overall design of the sheet, the naming scheme, etc, there's really no way to have the system call all pages or subforms starting with the same letters and such. So I'd have to name each one individually. And that's fine! But I've tried a million methods to no avail. I just want a more condense way to hide/make visible multiple pages and subforms simultaneously, or barring that to reset the visibility to the default.


Solution

  • The nice folks over at the Adobe forums were very helpful and provided the following answer:

    With an array and the forEach() method you can write a very compact script to do what you're after.

    var aObjects = [[subpage_1, "1"], [subpage_2, "2"], [page_3", "3"]],  
      cSelection = xfa.event.change;  
    // Loop through all elements in the array…  
    aObjects.forEach(function (element) {  
        //… and set its presence  
        // element[0] is the first value of the nested array i.e. "subpage_1", element[1] is the second value i.e. "1"  
        element[0].presence = element[1] === cSelection ? "visible" : "hidden";  
    });