Expand my Community achievements bar.

dynamically create new table with set number of rows

Avatar

Level 4

I have a form to collect data from a region on a quarterly basis. Data is collected in a dynamic table with the ability to report, by county, in expandable rows in the table. This functionality is working fine.

I want to dynamically create a second table (based on the first table). I have set up a button to dynamically creates an exact replica of the first quarter table for use in filling in second quarter data. I would like to ensure that when the second quarter table is created, the entries from table 1 are populated in the second table. I think I need to use some type of a counter and when creating the second table, use the counter to establish number of rows in the second quarter table. I also want to populate the county column so the user does not have to re-enter. I see code from a similar thread on how do that part, but do not know how to dynamically and correctly created the second quarter table. The table is wrapped in a subform. The add new quarter button code is simple javascript:

_UPT_Services.addInstance(1)

Screen shot:

Screenshot 2018-03-20 14.27.22(2).png

Appreciate any help members can provide.

3 Replies

Avatar

Level 7

You could use the instance manager of the first table to see the count for how many instances (rows) there are.

Eg: Table1.instanceManager.count

You could then use switch or if to add rows based on the instance manager count in the button to add the second table. You would then prefill the second table based on the first.

Avatar

Level 4

I am a relatively novice in coding and have noticed you answer a lot of questions here. Any chance you could provide a bit of code on how to implement that?

Avatar

Level 7

Here is an example to help you understand instance referencing.

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

In my example i have a

  • duplicate button - to duplicate the table
  • a numeric field to keep track of the instances
  • a table with single repeating row and an add row button
  • I also have another table (hidden) with repeating row and add row button (not really needed)

1447263_pastedImage_0.png

Add Row Button - click event javascript

//add a row to table1

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

if (xfa.host.version < 8) {

xfa.form.recalculate(1);

}

//with each click show the current instance in NumericField1

NumericField1.rawValue = Table1.Row1.instanceManager.count;

For the purposes of testing,

i added initialize code to the textfield in the table

this.rawValue = Table1.Row1.instanceManager.count; //when a new row is created, so is a textfield and it gets the value of the instance.

Duplicate Button - click event javascript

//show table2

//logic to determine the amount of instances (rows)

//logic to duplicate textfield contents into table2.

switch(NumericField1.rawValue)
{
case 2: //if NumericField1's value is 2, execute this code

Table2.presence = "visible"; //show the hidden table
this.resolveNode('Table2._Row1').addInstance(1); //add one row only because the table starts with 1 row initially
this.resolveNode("Table2.Row1[0].TextField1").rawValue = this.resolveNode("Table1.Row1[0].TextField1").rawValue; //give the textfield in table2 the same as the value in table1. Row1 is instance 0 - [0]
this.resolveNode("Table2.Row1[1].TextField1").rawValue = this.resolveNode("Table1.Row1[1].TextField1").rawValue; //give the textfield in table2 the same as the value in table1. Row2 is instance 1 - [1]
break;

case 3: //if NumericField1's value is 3, execute this code

Table2.presence = "visible"; //show the hidden table

this.resolveNode('Table2._Row1').addInstance(1);
this.resolveNode('Table2._Row1').addInstance(1);
this.resolveNode("Table2.Row1[0].TextField1").rawValue = this.resolveNode("Table1.Row1[0].TextField1").rawValue; //give the textfield in table2 the same as the value in table1. Row1 is instance 0 - [0]
this.resolveNode("Table2.Row1[1].TextField1").rawValue = this.resolveNode("Table1.Row1[1].TextField1").rawValue; //give the textfield in table2 the same as the value in table1. Row2 is instance 1 - [1]
this.resolveNode("Table2.Row1[2].TextField1").rawValue = this.resolveNode("Table1.Row1[2].TextField1").rawValue; //give the textfield in table2 the same as the value in table1. Row3 is instance 2 - [2]
break;

case 4: //if NumericField1's value is 4, execute this code

Table2.presence = "visible"; //show the hidden table

this.resolveNode('Table2._Row1').addInstance(1);
this.resolveNode('Table2._Row1').addInstance(1);
this.resolveNode('Table2._Row1').addInstance(1);
this.resolveNode("Table2.Row1[0].TextField1").rawValue = this.resolveNode("Table1.Row1[0].TextField1").rawValue; //give the textfield in table2 the same as the value in table1. Row1 is instance 0 - [0]
this.resolveNode("Table2.Row1[1].TextField1").rawValue = this.resolveNode("Table1.Row1[1].TextField1").rawValue; //give the textfield in table2 the same as the value in table1. Row2 is instance 1 - [1]
this.resolveNode("Table2.Row1[2].TextField1").rawValue = this.resolveNode("Table1.Row1[2].TextField1").rawValue; //give the textfield in table2 the same as the value in table1. Row3 is instance 2 - [2]
this.resolveNode("Table2.Row1[3].TextField1").rawValue = this.resolveNode("Table1.Row1[3].TextField1").rawValue; //give the textfield in table2 the same as the value in table1. Row4 is instance 3 - [3]
break;

default: //if NumericField1's value is not any of the previously specified cases, execute this code
//enter code to execute if the logic above is not matched.
break;
}

Notes:

  • In this example, a value of 0 or 5 or more in NumericField1 will make the switch refer to the default code to be executed, which in this case is nothing.

Clicking Duplicate does nothing as the Instance Count numeric field does not meet the switch logic.

1447264_pastedImage_18.png

I added 3 rows in table1, then clicked Duplicate. Table2 is shown, two more rows are added, text from table1 is duplicated to the correct rows in table2.

1447280_pastedImage_19.png

This is by no means bulletproof code, there probably are other logic considerations, but it should give you a good start with referencing instances.