Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Copy table values to new instance of table

Avatar

Former Community Member

Good Morning.

I have a flowed form that looks like this.

form1

        Subform1

                       ACT (ExpandableTable)

                              Header

                              Row1

                              Row10

                              Footer

                                         button

when the button in the footer is clicked a popup menu appears where users can insert a new instance of the Table, Copy current section, or delete current section. I have not been able to copy the values from the previous instance successfully when the copy current section is chosen. All fields need to be copied and there is probably 20 fields in all. Also how would I insert a new blank instance between two instances?

Here is the click event I have so far.

var cChoice = app.popUpMenu("Add a blank section",  "Copy this section", "-", "Delete this section");

if (cChoice == "Delete this section"){

_ACT.removeInstance(ACT.instanceIndex);

}

else if

(cChoice == "Add a blank section"){

_ACT.addInstance(ACT.instanceIndex);

} else {

var intCount = xfa.resolveNode("ACT").instanceManager.count; xfa.resolveNode("ACT").instanceManager.addInstance(1);

xfa.resolveNode("ACT.Row1[" + intCount.toString() + "].AssetClass").rawValue = xfa.resolveNode("ACT.Row1[" + (intCount - 1).toString() + "].AssetClass").rawValue ; }

17 Replies

Avatar

Level 7

This is an interesting problem. I am looking at it now. For adding and instance you would need to use:

Table1.Row1.instanceManager.insertInstance. You will need to get the index number and determine where to insert. I am attaching a link to a good blog on the topic.

Avatar

Former Community Member

I have no problem creating a new instance.  The problem is copying the values from the previous instance once the new instance has been created.

Avatar

Level 7

I am trying to figure that out. You had also asked about inserting a row and I was providing that info for you.

Avatar

Former Community Member

Oh ok that is what I though but figured I would clarify just in case.

Thanks


Avatar

Level 7

I did a basic test and was able to get it to work using the following simple script:

xfa.resolveNode("form1.#subform.Table1.Row1[1].Name").rawValue = xfa.resolveNode("form1.#subform.Table1.Row1").Name.rawValue

You will need to get the index of the row you want to copy and then you would need to programmatically replace the number inside the [ ] with the correct index number. You probably also want to create a loop to go through all the fields on that row and pull them over. if it is just a couple fields then you could just do a line for each.

Avatar

Former Community Member

I still can't seem to get it to work. When I select 'copy this section' it just inserts a blank instance. In case I didn't explain it correctly when a user inserts an instance they are actually inserting a new instance of the ACT table which has a header, footer and nine rows of data.

Also I think I have done something wrong and am throwing the instance manager out of sync when selecting the other choices.

Here is my updated script.

var cChoice = app.popUpMenu("Insert a blank section above", "Insert a blank section below", "Copy this section", "-", "Delete this section");

if (cChoice == "Delete this section"){
_ACT.removeInstance(ACT.instanceIndex);
xfa.form.recalculate(1);
}
else if
(cChoice == "Insert a blank section below"){
var RowCount = _ACT.count - 1
_ACT.addInstance(RowCount + 1);
xfa.form.recalculate(1);
}
else if
(cChoice == "Insert a blank section above"){
var RowCount = _ACT.count - 1
_ACT.addInstance(RowCount);
xfa.form.recalculate(1);
}
else
{
_ACT.addInstance(ACT.instanceIndex);
var vRowCount = _ACT.instanceIndex - 1
var i=0;
    for (i=1;i<=vRowCount;i++)    
    { 
    xfa.resolveNode("form1.#subform.ACT[" + (i-1) + "].AssetClass").rawValue = xfa.resolveNode("form1.#subform.ACT.Row1").AssetClass.rawValue
}
}




Avatar

Level 7

I was able to get it working with the following. Right now there is no condition like you have in your script so mine replicates for each click.

var i = this.parent.index 

xfa.resolveNode("form1.#subform.Table1.Row1[" +(i+1) + "].Name").rawValue = xfa.resolveNode("form1.#subform.Table1.Row1").Name.rawValue

Avatar

Former Community Member

Still not working. New instance created but it is not carrying over the values from the text field.

_ACT.addInstance(ACT.instanceIndex); // Add a new instance(row) to the table

var i=this.parent.index;

    xfa.resolveNode("form1.#subform.ACT.Row1[" + (i+1) + "].AssetClass").rawValue = xfa.resolveNode("form1.#subform.ACT.Row1").AssetClass.rawValue

Avatar

Level 7

I think I figured out how to do it. I do not have all the mechanics of it but I think it can work. What you need to do is remove rows 2-9. Have only 1 row but in the binding tab tell LCD to make that 9 instances. LCD will create a row[0] .. row[8] then set the field names across to something like assetClass for each of the fields so  your field names would be assetClass[0].. assetClass[9]

now all you have to do is a loop to iterate through the field name and replace a var i for example with the numerical sequence 0, 1, 2, etc.

Essentially while i < 10 do...

You will need to replicate this for each row however I think you can handle that with a second loop that would set another variable like j to replace the row number.

Does that make sense?

Avatar

Former Community Member

That won't work because in each row the column widths and number of columns are different. Some columns have been merged to provide a larger cell area.

Avatar

Level 7

That should not matter. Ultimately you are taking table1, row1, column1 and moving it to table2, row2, column2. the only thing you will need to do is to loop through the list so that you replace the numbering table[1] row[1] column[1] to table[2] row[2] column[2]

Avatar

Former Community Member

Ok maybe I am confused because you said to remove rows 2-9 and have LCD replicate the rows. Wouldn't that mean that all rows will mirror the original row? Can you post an example and I will try it?

Avatar

Level 7

If your table rows need to be individually set then yes that will make it a bit harder but you can still do the same loop a row at a time. Your loop will iterate through each field in the row

Avatar

Former Community Member

Mike,

I was finally able to get it to work except it will only copy the values on the first time. If I try and copy a second time it only inserts a blank instance. All of the text fields are nested in a subform named data do think that is throwing off the index?

var cChoice = app.popUpMenu("Add a blank section",  "Copy this section", "-", "Delete this section");

if(cChoice == "Copy this section"){

ACT._Row1.addInstance(1)

var i = this.parent.instanceIndex 

xfa.resolveNode("form1.#subform.ACT.Row1[" +(i+1) + "].data.AssetClass").rawValue = xfa.resolveNode("form1.#subform.ACT.Row1.data.AssetClass").rawValue

}

else

if(cChoice == "Add a blank section"){

ACT.Row1.instanceManager.addInstance(1)

}

else

if(cChoice == "Delete this section"){

ACT.Row1.instanceManager.removeInstance(this.parent.instanceIndex)

}

Avatar

Level 7

You would need to step through all your rows. This will work on the row you are adding but my understanding is you want to copy the whole table.

Avatar

Former Community Member

Following your advice I changed my method of approach. I am now inserting a new instance of Row1 which was merged into one subform (data) that contains all of the data fields(42 in total) that are required. I only posted the script for one textfield which is all I am testing it on until I get it working. Why does it only work the first time?