Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How to insert table rows dynamically?

Avatar

Former Community Member

I have a table with 18 rows. Each of the first 17 rows have read-only text (mandatory activities) in a text field in the first column; the last row is a blank row with an add button so the user can add a new activity to the bottom of the table.

I've been asked to allow users to insert a new activity at any location in the table (for example, to insert a new Row 7 after Row 6), instead of just at the end of the table. (Obviously this also requires removing the text from the activity text box and changing it to User Entered, as well as a couple other housekeeping activities, which shouldn't be a problem if I can just get the darn row to insert!)

I've searched the forums and reviewed some of Assure Dynamics excellent examples. From what I've seen, this should be relatively straightforward...but it's not working.

Or rather, it works fine in the first row, multiple times even. I've got a message box displaying the current and new row indices correctly for every row. But in any other row than the first, insertInstance() gives me an "index value is out of bounds" error even when the message box shows the correct indices.

Given the following structure and code in each row's ActivityAdd button, can anybody tell me what I"m doing wrong? Thanks!!!

-------------------------------------------

tableStructure.png

---------------------------------------------

T_Activities.R_Activity[0].T_AddRemove.R_Add.B_ActivityAdd::click - (JavaScript, client)

var vNewRowIndex = this.parent.parent.parent.index + 1;

xfa.host.messageBox("This row index: " + this.parent.parent.parent.index + ". New row index: " + vNewRowIndex + ".");

T_Activities.R_Activity.instanceManager.insertInstance(vNewRowIndex, 1);

------------------------------------------------

FWIW, I've seen the cautions regarding the instance manager and underscores in object names...for better or worse, *all* of my object names in *all* of my forms use underscores like this, and to the best of my knowledge I've never had a problem. If that turns out to be the problem here, I'll gladly change the names in the table.

1 Accepted Solution

Avatar

Correct answer by
Level 7

Were you using java or formcalc when you got the error? I recreated your table and the code works in formcalc.

In regards to the insertIndex working in the first row I think that may be because you are always referencing the first row (R_Activity).

View solution in original post

8 Replies

Avatar

Level 7

You could just do (in formcalc):

T_Activities.R_Activity[$.parent.parent.parent.index].instanceManager.addInstance()

Avatar

Former Community Member

Thanks...that seems like an intriguing idea but it gives me a "T_Activities.R_Activity[$.parent.parent.parent.index] is undefined" error, as does simply harcoding the index of the test row into the brackets (which is obviously not a good solution for many reasons, but at least I might understand table reference syntax a little better...).

Clearly I'm missing some key piece of information here; I assume it has to do with referencing table rows. I do think it's odd that myoriginal insertIndex script works correctly in the first row of the table, *and* in each row added from that first row, but it doesn't seem to work anywhere else in the table...

I'm going to try rebuilding the table using subforms instead of table rows, and see if that gets me anywhere, but I'm still very interested in any other ideas for this table row model.

Thanks!

Avatar

Correct answer by
Level 7

Were you using java or formcalc when you got the error? I recreated your table and the code works in formcalc.

In regards to the insertIndex working in the first row I think that may be because you are always referencing the first row (R_Activity).

Avatar

Former Community Member

All the rest of my scripts are in javascript, but I didn't know I could also mix in some FormCalc...so yes, it now inserts the row in the correct place, THANK YOU!!

And I was able to strip out the read-only text in the initial row and set the text field to "open."

However.

Everything breaks when I save and reopen the form.

  • *ALL* of the table rows have been duplicated at the top of the form.
  • The text I entered in the new row's text field  is now in a read-only field in a row at the beginning of the original rows.
  • the "open" text field is still in the row I added, but its text field now contains the text that was in the read-only row below the origial row (if that makes any sense at all).

And the same thing happens when I just add rows to the first row, which still has my original js code.

Thanks for your help, why, I really appreciate it. I've spent so much time in this rabbit hole that I think I have to defer this feature to the next revision cycle, and do some more research in the meantime. I clearly need to learn more about the instanceManager and table/row objects...I do wish there was some decent information out there for us pseudo-scripters. I really do know just about enough to get myself in trouble, but not enough to get out of it! Any suggestions?

Thanks again!!

Avatar

Level 7

the repeating rows when you reopen the form probably have something to do with the binding. Under Object > Binding just make sure the 'Repeat Row for each data item' box is only clicked on the actual row you are repeating and not the whole table.

Avatar

Former Community Member

Hmmm...no, that's set correctly -- unchecked for the table, checked for each row.

Avatar

Level 7

You have a table within a table so maybe check for the rows/table within each one

Avatar

Former Community Member

You mean the table in the last column that holds the buttons? No, those are set correctly too, and I just tried putting the buttons into a subform instead of a nested table, and the it does the same thing. I was hoping that had something to do with it...