Expand my Community achievements bar.

SOLVED

Slight issue with removeInstance

Avatar

Level 1

I have a form with a repeating subform. The coding I use for the 'Add SubForm' button is;

topmostSubform.ContinuationSheet.ButtonAddSheet::click - (JavaScript, client)

ContinuationSheet.instanceManager.addInstance(1);

ButtonReset.presence = "hidden";

ButtonSaveAs.presence = "hidden";

ButtonPrint.presence = "hidden";

ButtonAddSheet.presence = "hidden";

When the reset button is pressed, it is suppose to not only reset all form data, but also reset the SubForm count back to 0. The coding I use for that is;

topmostSubform.ContinuationSheet.ButtonReset::mouseUp - (JavaScript, client)

xfa.host.resetData();

while (ContinuationSheet.instanceManager.count > 0)

{

    ContinuationSheet.instanceManager.removeInstance (1);

}

It all works as expected, mostly. The only issue I am having is that the buttons in the original SubForm remain hidden. I have tried many different scripts to try and get them to show back up, but to no avail. Any help would be very much appreciated.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Yeah like I said that was just a quick idea. The code needs to be more along the lines of checking for the last instance and then displaying but not displaying if it isn't the last instance.

So why are you hiding the buttons instead of just letting them float after the repeating subforms? You could have the buttons in their own subform that lives inside the flowed subform but outside of the repeating instances so it always shows up at the end.

Ah, I think I got it:

if (this.parent.instanceIndex == this.parent.instanceManager.count - 1) {

          this.presence = "visible";

}

else {

          this.presence = "hidden";

}

You need to subtract one from the count because it's 1-based but the index is 0-based.

Tested it and it seems to work ok. You may have to play with the "parents" (this.parent.parent...etc) depending on how buried the subform holding the buttons is. If you leave them in a subform then you could put this code on the subform that holds the buttons instead of on the buttons themselves.

Oh and I put that on the layout:ready event.

Message was edited by: Jono Moore - added event info

View solution in original post

8 Replies

Avatar

Level 10

Hi,

Try a xfa.form.remerge() or manually script the presence of the buttons in the click event of the reset button.

Also for the ContimuationSheet you can use setInstances(0) instead of the loop, eg

_ContimuationSheet.setInstances(0);

The parameter being the number of instances you want.

Niall

Message was edited by: Niall O\'Donovan: Sorry setInstances() is plural!

Avatar

Level 1

Neither one of those work period. Also, for some reason, the _ shortcut doesn't work.

Avatar

Level 10

You could try setting the count value instead of looping or setInstance as well. I just did this on some table rows. The _ shortcut should work - don't forget you need the path to the repeating object.

pathToObject._ContinuationSheet.count = 0;

or

pathToObject.ContinuationSheet.instanceManager.count = 0;

And as Niall mentioned above, you'll probably have to code the buttons presence back to visible.

Avatar

Level 1

The loop works fine. In fact, the scripts I posted above work exactly as I need them to. What doesn't work is getting the buttons to switch back to visible (screen only) presence when the reset button is pressed. Scripting the buttons again in the click event doesn't work. I am at a loss as how to get the buttons to reappear on the original after the instances are removed. Below is the code I attempted to use, both with and without the .relevant;

topmostSubform.ContinuationSheet.ButtonReset::click - (JavaScript, client)

ButtonReset.presence = "visible";

ButtonReset.relevant = "-print";

ButtonSaveAs.presence = "visible";

ButtonSaveAs.relevant = "-print";

ButtonPrint.presence = "visible";

ButtonPrint.relevant = "-print";

ButtonAddSheet.presence = "visible";

ButtonAddSheet.relevant = "-print";

Avatar

Level 10

So maybe we need to look at this differently - why are you hiding the buttons? Are they in the repeating subform and if so why not put them after the repeating subform so they just float?

If they are in the repeating subform you could maybe try something like referencing the count value...on the initialize or layout:ready events (not sure what will work for this you will need to experiment)...I'm just guessing at the code below, haven't tested it - but using something like this you could then get rid of the code that explicitly hides the buttons.

(if they are in the repeating subform then I'm guessing you want a count of 1)

if (this.parent.instanceManager.count == 1) {

     this.presence = "visible";

}

else {

     this.presence = "hidden";

}

Can you post your form?

Avatar

Level 1

Unfortunately I am not allowed to post the form. I wrapped the buttons into a subform and applied your coding to the init event. I had to change it from == to => in order for it to appear on further pages. The problem with this is it now doesn't disappear at all. I really appreciate the help.

Avatar

Correct answer by
Level 10

Yeah like I said that was just a quick idea. The code needs to be more along the lines of checking for the last instance and then displaying but not displaying if it isn't the last instance.

So why are you hiding the buttons instead of just letting them float after the repeating subforms? You could have the buttons in their own subform that lives inside the flowed subform but outside of the repeating instances so it always shows up at the end.

Ah, I think I got it:

if (this.parent.instanceIndex == this.parent.instanceManager.count - 1) {

          this.presence = "visible";

}

else {

          this.presence = "hidden";

}

You need to subtract one from the count because it's 1-based but the index is 0-based.

Tested it and it seems to work ok. You may have to play with the "parents" (this.parent.parent...etc) depending on how buried the subform holding the buttons is. If you leave them in a subform then you could put this code on the subform that holds the buttons instead of on the buttons themselves.

Oh and I put that on the layout:ready event.

Message was edited by: Jono Moore - added event info

Avatar

Level 1

Nice! That works wonderfully. Thank you very much!