Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.

How to scan a form of all buttons to hide them

Avatar

Level 2

Is there a way to scan for all the buttons within a form and render them hidden.  I am creating a form which has multiple buttons on it.  We plan to electronically deliver these forms to the customer but would like the buttons removed before the form is sent.  To accomplish this I have placed a 'Finanlize Form' button at the bottom of the form.  The plan is for this button to make the form read-only and remove all visible buttons.

The 'Finalize' button works with static buttons in the form.  But there is a section in the form that has dynamic subforms with buttons in them.  These are subforms that the user is able to add or remove as needed.  I created a 'for' loop to try to count the number of subforms that were added/removed by the user when the form is finalized but it does not seem to be working.

Here is the current script:

//This line makes sure all calculated fields are current before the form is finalized.

xfa.form.recalculate(1);

//This line renders all fields 'Read-only'.  There is a function that is called with this line.
myScriptObject.LockAllFields(form1);

//These lines render static buttons in the form invisible.  These lines are working.

form1.quoteTable.Table.Footer.Button1.presence="invisible";
form1.quoteTable.Table.Footer.Button2.presence="invisible";

//This if statement determines whether a button called 'Enclosures' (located inside subform enclosuresForm) is visible.

if (form1.enclosuresForm.presence != "hidden")
    {

//If the form is visible, then it is rendered hidden.
    form1.enclosuresForm.presence="hidden";   
    }

//This is where I try to begin counting buttons within the subform, 'CCForm'.

//This variable counts the number objects within CCForm. 'CCForm' has 3 child subforms within it, initially.

var CCFormCounter = CCForm.nodes.length;

//This variable is used as a counter in the 'for' loop declaration
var j;

//This variable is used to keep track of the current object within the following for loop

var cElement;

//This for loop looks at all objects within subform 'CCForm'
    for(j=0; j< CCFormCounter;j++)
    {
    cElement = CCFormCounter.item(j);
//The following if statement determines if the object is a button and whether the button is already hidden
        if((CCFormCounter.item(j).className == "button") & (CCFormCounter.presence != "hidden"))
        {

//If the object is a button and not hidden then the button is set to 'hidden'.
        cElement.presence="hidden";
        }
    }

//This is the 'Finalize' button itself, which is also hidden

form1.Button6.presence="hidden";

5 Replies

Avatar

Level 7

Instead of this:

if((CCFormCounter.item(j).className == "button") & (CCFormCounter.presence != "hidden"))


        {

//If the object is a button and not hidden then the button is set to 'hidden'.
        cElement.presence="hidden";
        }
    }

try this:

if(CCFormCounter.item(j).className == "button" && CCFormCounter.presence != "hidden")
        {

//If the object is a button and not hidden then the button is set to 'hidden'.
        cElement.presence="hidden";
        }
    }

Avatar

Level 2

Still no luck.  I suspect that there is no className called "button".

Avatar

Level 2

I've adapted the "Lock all Fields" function to try to scan for buttons within my form.  I've disocvered that buttons are child objects of 'ui' which, in turn, is a child object of 'field'.  Looking at the "Lock all Fields" script I've noticed that there's an 'if' statement that determines if a 'field' is a radio button (exclGroup).  If an object is a radio button then a new 'if' statement further scans for a field ('field' is a child of 'exclGroup').  Using this logic I adapted the code to find button objects in the same manner.  First by looking for fields, then ui, then buttons.  I tried the code but it is still not working.  The code is shown below.  Is there something I am missing or is my concept, of how things are working, wrong?

form1.#variables[0].myScriptObject2 - (JavaScript, client)
/*************************************************************************************
Function: HideSomeFields
Description: This function will hide some fields.
IN: The parent subform. It could also be an element that contains subform like form1
OUT : nothing
**************************************************************************************/
function HideSomeFields(myParentObject2)
{
    var allChildElements;
    var intNumElements;
    var currentElement;
    var j;
   
    //Get all the child nodes of the parent element
    allChildElements = myParentObject2.nodes;

    //Total number of element in the object
    intNumElements = allChildElements.length;
   
    //Loop through all the child elements
    for(j=0; j< intNumElements;j++)
    {
        currentElement = allChildElements.item(j);
        //If the element is subform the function is repeated

       if(allChildElements.item(j).className == "subform")
        {
            HideSomeFields(currentElement);
        }
        //This 'else if' statement first determines if the object is a field

       else if(currentElement.className == "field")
        {
            for(k=0; k< currentElement.nodes.length;k++)
            {

               //If the object is a field.  This 'if' statement determines if the object is a 'ui'
                if(currentElement.nodes.item(k).className == "ui")
                {
                    for(l=0; l< currentElement.nodes.length;l++)
                    {

                    //If the object is a 'ui', this 'if' statement further determines if the object is a button
                        if(currentElement.nodes.item(l).className == "button")
                        {

                            //If the object is a button then this line hides the button.
                            currentElement.presence = "hidden";
                        }
                    }
                }           
            }   
        }
    }
}//end function

Any assistance is appreaciated.  Thanks.

Avatar

Level 10

Yes, you are correct. All the form fields have their class name as field not just button directly.

To be more clear, see the following structure of a button:

----------------------

<field name="Button1" y="34.925mm" x="63.5mm" w="28.575mm" h="6mm">

            <ui>

               <button highlight="inverted"/>

            </ui>

            <font typeface="Myriad Pro"/>

            <caption>

               <value>

                  <text>Button</text>

               </value>

               <para vAlign="middle" hAlign="center"/>

               <font typeface="Myriad Pro"/>

            </caption>

            <border hand="right">

               <?templateDesigner StyleID apbx2?>

               <edge stroke="raised"/>

               <fill>

                  <color value="212,208,200"/>

               </fill>

            </border>

</field>

-------------------------

Each xml tag is a class. So you must verify the button using the following expression in If condition:

if(XXXX.ui.nodes.item(0).className == "button")

{

     // Hide the button

}

Nith

Avatar

Level 2

I went with a different approach because the function was frustrating me.  I have figured out a solution using for loops that seems to be working so far.  Here is what was done:

I had a set of subforms I wanted the code to comb through to find a dynamically created button.  Here is how the subforms were organized:

<form1>

     <CCForm>

          {CCField1}[Button7]

     <blankCC>

          {blankCCField}[Button4]

     <addCCButton>

          [addCC]

<> = subforms

{} = text fields

[] = buttons

Users were able to dynamically add instances of the subform 'blankCC' in the document.

Now here is the code that scanned the document for the dynamically generated portions of the document:

//Creates two variables.  One, intNewIndex2, sets the limit in the 'for' loop.  The other, intNewIndex, is used to keep track of the current button within the subform.

    var intNewIndex2 = form1.CCForm.blankCC.instanceManager.count;
    var intNewIndex  = form1.CCForm.blankCC.instanceManager.count - 1;

//Start of the 'for' loop.  The loop begins with the number zero (nCount = 0) and increments by one (nCount ++) each time the loop is completed.  The loop will terminate when nCount is equal to intNewIndex2.

    for(var nCount = 0; nCount < intNewIndex2; nCount ++)
    {

//The next line searches the subform for dynamically created subforms named 'blankCC' (blankCC[0], blankCC[1], blankCC[2]...etc.).  Each instance of the subform blankCC contains a button in it named 'Button4'.  Button4's presence is set to hidden.
    form1.CCForm.resolveNode("blankCC[" + intNewIndex + "]").Button4.presence="hidden";

//This line moves to the next incidence of 'blankCC' in the document by subtracting one from the total number counted when the variable was first declared.
    intNewIndex = intNewIndex - 1;
    }

Thanks for your help Nith.  I did not get a chance to test your suggestion because I had already changed my approach to solving it, but I will keep this in my notes for later use.