Expand my Community achievements bar.

Adding/removing rows dynamically from a table with buttons

Avatar

Level 5

I have an expanding table which will be used to enter a list of user names.  Each row has a "Remove User" button in the last cell on the right.  At the top of the table I have an "Add More Users" button.  I would like to hide the "Remove User" button when when the form is first opened or if there is only one user. Then after you click "Add More Users" I would like the button to start showing on all rows, including the first.  I'd greatly appreciate any help.

Table crop.jpg

The Add More Users button has this script, but it isn't working right:


if (RowUserName.RemoveUser.presence = "visible") {


  this.resolveNode('Table1._RowUserName').addInstance(1);


  }


  else


  RowUserName.RemoveUser.presence = "visible";


  this.resolveNode('Table1._RowUserName').addInstance(1);






I also tried using a small rectangle to block the label on the first button ("BlockLabelOnFirstButton"), but that doesn't work either (it completely disables the "Add New Users" button):


this.resolveNode("BlockLabelOnFirstButton").presence = "invisible";


this.resolveNode('Table1._RowUserName').addInstance(1);


if (xfa.host.version < 8) {


  xfa.form.recalculate(1);


  }


if (RowUserName.instanceManager.count = 1)


  {


  this.resolveNode("BlockLabelOnFirstButton").presence = "visible";


  }


3 Replies

Avatar

Level 7

This might be more easily accomplished by setting the minimum count of RowUserName to 1.

748603_pastedImage_0.png

The button would remain, but it wouldn't do anything.

As for adding the user row, your if statement has an assignment rather than a condition. You need to use == instead of =. Here are the scripts I used to make your example work for me.


form1.p1.Table1.HeaderRow.AddUser::click


Table1.RowUserName.instanceManager.addInstance();


xfa.resolveNode("Table1.RowUserName["+0+"].RemoveUser").presence = "visible";



form1.p1.Table1.RowUserName.RemoveUser::click


this.parent.instanceManager.removeInstance(this.parent.instanceIndex);



form1.p1::ready:layout


//the following is not strictly needed if you set the min count to 1 since you can't remove the last instance anyway.


if(p1.Table1.RowUserName.instanceManager.count == 1) xfa.resolveNode("Table1.RowUserName["+0+"].RemoveUser").presence = "hidden";


else xfa.resolveNode("Table1.RowUserName["+0+"].RemoveUser").presence = "visible";


Avatar

Level 5

jasotastic81, thanks for your response.  I haven't been able to get that code to work.  I'm using LCD ES2 and opening the resulting PDF in Acrobat X.  The Add User button has no effect.

temp.png

I have the Remove User button initially set to Hidden.

In the Add Users button, I added your script, but had to tweak the name a little bit to match my object hierarchy.  I also reversed the order of the two lines, figuring the Remove User button has to be made visible before the row gets copied -- does that make sense?


xfa.resolveNode("Page1.UsersRequiringAccess.Table1.RowUserName["+0+"].RemoveUser").presence = "visible"; // Set the button to visible before copying the row


Page1.UsersRequiringAccess.Table1.RowUserName.instanceManager.addInstance();








In the Remove User button, I added:


xfa.resolveNode("Page1.UsersRequiringAccess.Table1.RowUserName["+0+"].RemoveUser").presence = "visible";


Page1.UsersRequiringAccess.Table1.RowUserName.instanceManager.addInstance();







In the form layout:ready event I added


if(Page1.UsersRequiringAccess.Table1.RowUserName.instanceManager.count == 1) xfa.resolveNode("Page1.UsersRequiringAccess.Table1.RowUserName["+0+"].RemoveUser").presence = "hidden"; 


else xfa.resolveNode("Page1.UsersRequiringAccess.Table1.RowUserName["+0+"].RemoveUser").presence = "visible";


This also brings me to a couple of questions:  What's the difference b/w having "()" or "(1)" at the end of "addInstance".  When I type in the script and use the automatic pop-up text, it adds "([b1])" at the end.

Also, what is the purpose of "["+0+"]" in your script?

Thanks again for your help.

Avatar

Level 7

--Breaking the string to add the 0 that way is probably not strictly necessary. It was out of habit since I am usually putting a variable in place of the number.

--The 0 in brackets is required when you have multiple instances of something. In your case, RowUserName. The first row is called RowUserName[0].

The addInstance function will optionally take a single parameter: a Boolean. So, by putting a 1 in the parenthesis, you're essentially saying addInstance(true). Since it's optional, I left it out.

I tried my version (which has only a table with buttons on it), and I was able to still use the add button to add a row. xfa.resolveNode looks for an item that matches the string provided. So, if your page isn't named Page1, that would stop it from finding the row to add. I would recommend using as little as possible to find the right table. E.g., if your table named Table1 is the only Table1, then you don't need Page1.UsersRequiringAccess for resolveNode to find it.

I think you might have pasted the wrong text here. The text I see is for adding a row, not removing one.