Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

reset number of subform based on drop-down value selected

Avatar

Former Community Member

Hello,

I have a questions.  I have a form called Request.  Inside Request form, there is a subform called MultiForm, which is repeatable.  Request form also has a drop-down DropdownList1.  MultiForm has 'Add Profile' and 'Remove Profile' buttons.

As the form is first launch, Request is displayed with DropDownList1, MultiForm and 'Add Profile' button. 

As the user click on 'Add Profile', a second MultiForm is insert.  On this second MultiForm, there is 'Add Profile' and 'Remove Profile' button. 

When the user change value of the DropDownList1, I want the number of MultiForm to be reset so that there is only one MultiForm present.  The 2nd, 3rd, etc., MultiForm is removed. 

Also, I want the 'Add Profile' button to display only on the first MultiForm, not the subsequent MultiForm.

I have come up with the scripts below:

//remove MultiForm  (added under Change event for DropDownList1.

function resetMyForm(myForm) {

var vCount = myForm.count;

if (vCount>1) {

  if (vCount==3) {

   myForm.removeInstance(2);

   myForm.removeInstance(1);

  } else if (vCount == 2) {

   myForm.removeInstance(1);

  } else {

   //do nothing

  }

} else {

  // do nothing

}

}

----- The reason this funciton works for me because I only allow max 3 MultiForms to be inserted and there is always one MultiForm on the page.  I'm looking for a more 'dynamic' script instead of the 'static' script above.  Any suggestion would be greatly appreciated. 

Also, I have another question:  I want the 'Add Profile' to be present on the first MultiForm only.  As new MultiForm added, I don't want the 'Add Profile' button to display on the subsequent MultiForm (except for the first form)

Is there a sample function that I can use?  Where should I insert that function to?

Thank you very much for your help.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

A quick workaround would be setInstances() method.

I am surprised that you are getting removeInstance() to work without calling instanceManager or the shorthand underscore (_).

_myForm.setInstances(0); // remove all instances

_myForm.setInstances(1); // create a new blank instance

Hope that helps,

Niall

View solution in original post

3 Replies

Avatar

Correct answer by
Level 10

Hi,

A quick workaround would be setInstances() method.

I am surprised that you are getting removeInstance() to work without calling instanceManager or the shorthand underscore (_).

_myForm.setInstances(0); // remove all instances

_myForm.setInstances(1); // create a new blank instance

Hope that helps,

Niall

Avatar

Former Community Member

Thank you, Niall. the code works like a charm now. 

Actually, when I call the script, I have to pass in _myForm.   Yeah, not sure why I don't need to use instanceManager as stated in adobe sample code.

Avatar

Level 10

Glad you have it working!

FYI, the underscore (_) is shorthand for instanceManager.

instanceManager works when there is at least one instance of the repeating object rendered on the form. For example, if you deselect the Min Count so that there isn't an instance when the form renders, then instanceManager will not work.

However the underscore will always work, even if there ins't an instance rendered when the script is called.

Hence, I always tend to use the shorthand,

Niall