These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Using removeInstance on a subform

Lea1337
Registered: Jul 14 2009
Posts: 64
Answered

I am kind of a newbie in LC Designer, and have come upon a problem that I'm not sure how to solve.

I have created a subform called NextOfKin that contains some textfields and radiobuttons. In addition I have an Add and a Remove button in another subform, these are intended to add and remove instances of the NextOfKin subform.

So far I have succeeded in making the Add button work by using the instanceManager and addInstance. I have also made sure that the Remove button only appears if you have more than one instance of the NextOfKin subform (using a counter).

However, I am stuck when it comes to scripting the click event on the Remove button, mostly because I don't know how to reference the most recent instance of the NextOfKin subform (to be able to remove it).

Can anyone help me with the syntax?

My Product Information:
LiveCycle Designer, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There's no such thing as the most recent instance, at least as far as the JavaScript model is concerned, i.e. there's no variable that saves this data. If you wanted to set things up this way you could save the latest instance Index to a custom member in a parent subform in the same code that creates the new instance. I do this kind of thing all the time.

However, the most common way to setup a delete button is to place it inside the repeatable subform. Then the instance to delete is the instance ID of the current subform.
The code should be something like this, in the click event for the delete button:

_NextOfKin.removeInstance(this.parent.index);

Have you see this video?
https://admin.adobe.acrobat.com/_a200985228/p87746471/

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Lea1337
Registered: Jul 14 2009
Posts: 64
Sorry for the late reply, I've been away on vacation. I have looked at the solution that places the delete button inside the repeatable subform, but in this case I wanted to keep the delete button at the top along with the add button (this looked better from a design point of view I thought).

So I would like to try your other solution, saving the latest instance index to a custom member in a parent subform in the same code that creates the new instance. I'm just not quite sure what you mean by that. So far I have created a counter (a numeric field, for debugging purposes) that I increment every time I add an instance of the subform. The counter itself is placed in the parent subform. I can see that the counter is incrementet correctly when I add an instance. But when it comes to the delete button, I'm still stuck. I have tried using the 'removeInstance' along with the counter index, but nothing happens.. :-p
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The count is built into instance manager for a subform so you don't need to create a separate counter. If the latest instance is always added by the "instanceManager.addInstance()" function then it's always added to the end of the subform list and there is no need to save the last instance added. I suggested saving the last index as a general solution to instances that might be inserted into the subform list.

So try this:

_MySubform.removeInstance(_MySubform.count-1);

The "_" prefixed to the subform name makes the name reference the instanceManager directly. Its a kind of shorthand for the instanceManager. "count" is the property of the instanceManager that keeps track of the current number of instances. But the indexes for the subform instances always start at 0, so the last index is the count minus one. This is probably what was giving you the problem with your own counter.

I have a very advanced repeatable subform sample that shows how to use checkboxes on each line for controling operations like deleting lines. It also does other advanced operations like sorting the subform list based on a file value in the subform. You'll find it here, but you have to be a member to download the file.

http://www.pdfscripting.com/public/programs/downloadsearch.cfm?searchtype=simple&searchmode=cat&keywords=recent&sortby=name&cat=LiveCycle%20Form%20SamplesThom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Lea1337
Registered: Jul 14 2009
Posts: 64
Again, sorry about a late reply, I'm working with this at work and have unfortunately been otherwise preoccupied lately. Trying hard now to remember all the details around my scripting problem :-P

I have tried your solution, using

_MySubform.removeInstance(_MySubform.count-1);

but still no luck getting rid of subforms.. Have no idea why this doesn't work. The add-button, as I mentioned before, works fine and has the following code:

xfa.form.form1.Main_subform.New_emp_subform.Nextofkin_subform.Nextofkin_subform2.instanceManager.addInstance(true);

(I have a lot of subforms, I know. Trying to keep positioning at the same time as having floating subforms)

The delete-button now has this code:

xfa.form.form1.Main_subform.New_emp_subform.Nextofkin_subform.Nextofkin_subform2.removeInstance(xfa.form.form1.Main_subform.New_emp_subform.Nextofkin_subform.Nextofkin_subform2.count-1);

I also tried your suggestion, with the '_' instead of the loooong reference:

_Nextofkin_subform2.removeInstance(_Nextofkin_subform2.count-1);

But still no luck. Sorry about being a pain in the ..., but I'm really stuck on this one.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
As you've so amply shown, XFA forms have a hierarchical structure. Because of this it's really, really important that you get the SOM (i.e. subform) paths correct. The naming and number of levels have to be perfect. Without looking at your form I can't really tell you how your particular path should be formed.

But there are some things you can do. First off, in the code you've shown above, where the full path is used, there isn't a reference to the "instanceManager". Since this full path worked with the "addInstance()" function is should work with the "removeInstance()" function. Just put the "instanceManager" reference in there. But make sure the spelling and path elements are correct.

Number 2, when something goes wrong the very first place to look for an error report is in the JavaScript Console. There are links to tutorials on using the console window in my signature block below. If you still can't get it working, then please let us know what's being reported in the console.

There are also some great LiveCycle Form scripting tutorials at www.pdfscripting.com that show exactly how to do what you want to do.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Lea1337
Registered: Jul 14 2009
Posts: 64
After a lot of back and forth I finally surrendered and ended up placing the "remove" button inside the repeatable subform, thus avoiding the entire problem. I figured that maybe it wasn't such a bad idea to be able to remove an exact subform, instead of having to remove the last one. And also, since I was struggling so much and weren't able to find out what was wrong (and the javascript console reported no errors, as I have so often seen..), I just had to solve this one the easier way and go on to the next problem(s) ;-)

But thank you for all the help! :-)