Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Problem with removing correct instance

Avatar

Level 3

I have made a form that has an add button and remove button in a subform. Currently when the remove button is clicked on in any instance it removes the first instance. What is required to have the remove button remove the subform it sits within?

1 Accepted Solution

Avatar

Correct answer by
Level 10

Okay,

In the hierarchy check the relative path from the remove button to the ontreatment subform. If, for example, the remove button is in another subform, you would need to take this into account when determining the index of the repeatable ontreatment subform:

_ontreatment.removeInstance(this.parent.parent.index);

Each "parent" refers to the container or subform.

Hope that helps,

Niall

View solution in original post

7 Replies

Avatar

Former Community Member

The instance reference this.parent.index. For example,

_detail.removeInstance(this.parent.index);

Steve

Avatar

Level 10

Hi,

The addInstance() has a parameter of 1/0 or true/false, which relates to whether the new instance will be merged with the data model. Most offten you will use 1 or true.

However when it comes removeInstance(), the parameter refers to the specific instance you want to remove using the zero-based numbering system of the instances.

If the removeInstance() button is in the row.subform, then the following will work:

// _ is shorthand for instanceManager

_Subform1.removeInstance(this.parent.index);

this.parent.index will return the instance of the subform that contains the button.

Niall

Avatar

Level 3

Hi,

Thanks for that. I have used that code and it still deletes the first record no matter which 'remove' button is clicked (when it is inside the subform on any subsequent added sections that are added by clicking on an add record button within each subform).

Sorry if this doesn't make sense.

Avatar

Level 10

Hi,

Can you share your script? Also include the names of the subforms. I'll get to it when I can.

Niall

Avatar

Level 3

I have this on the 'click' event to add an instance of the subform.

_ontreatment.addInstance(true);

xfa.form.recalculate(true);

and I have this on the 'click' event wanting to remove that particular subform instance.

_ontreatment.removeInstance(this.parent.index);

xfa.form.recalculate(true);

the subform is called 'ontreatment'

and I have this on the 'click' event wanting to remove that particular subform instance.

Both buttons sit inside the subform. I need the user to be able to add a record and be able to delete that record by choosing the remove button.

Thanks for your help.

Avatar

Correct answer by
Level 10

Okay,

In the hierarchy check the relative path from the remove button to the ontreatment subform. If, for example, the remove button is in another subform, you would need to take this into account when determining the index of the repeatable ontreatment subform:

_ontreatment.removeInstance(this.parent.parent.index);

Each "parent" refers to the container or subform.

Hope that helps,

Niall

Avatar

Level 3

Thanks for that Niall - works perfectly.