Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Add a table row between rows and keep information entered in previous row

Avatar

Level 6

Good day,

I posted two questions which were answered by Radzmar below.  Thank you!  I have received information from the users that there are two issues.  (1).  When you add a row, the question asks how many rows to add.  If you click the cancel button, it still adds a row.  It should NOT add a row when the cancel button is clicked.  And (2) when you enter the number of rows to add, it adds the rows to the bottom of the table.  The rows need to be added below the row of the button pressed.  Your help in fixing these errors is appreciated.  I tried changing the script but I have not been successful.

 

var cInput = xfa.host.response("Enter number of rows you want to add:", "Add rows", "1", false),
	n = parseInt(cInput, 10) > 0 ? parseInt(cInput, 10) : 1,
	i = 0;
for (i; i < n; i += 1) {
	_Row1.addInstance(true);
}

To copy data, use a for loop.

Given you have the following structure: Table1.Row1.Date and the Add button is located in the Row1 as well then this script will copy the date fields data to all following rows starting from the current one and where the date field is empty.

if (!Date.isNull) {
	var oRows = Table1.resolveNodes('Row1[*]'),
		j = this.parent.index,
		cDate = Date.rawValue;
	for (j; j < oRows.length; j += 1) {
		if (oRows.item(j).Date.isNull) {
			oRows.item(j).Date.rawValue = cDate;
		}
	}
}

 

1 Accepted Solution

Avatar

Correct answer by
Level 10

What's the problem? You just have to to the same steps for ssfs as for dated.

 

var cInput = xfa.host.response("Enter number of rows you want to add:", "Add rows", "1", false),
	oRow = this.parent.parent,
	nIndex = oRow.index,
	cDate = oRow.dated.rawValue,
        cSsfs = oRow.ssfs.rawValue;
	n = parseInt(cInput, 10) > 0 ? parseInt(cInput, 10) : 1,
	i = 0;
if (cInput !== null) {
	for (i; i < n; i += 1) {
		_Row1.addInstance(true);
		_Row1.moveInstance(_Row1.count - 1, nIndex + 1)
		oRow.resolveNode('Row1[' + (nIndex + 1) + ']').dated.rawValue = cDate;
oRow.resolveNode('Row1[' + (nIndex + 1) + ']').ssfs.rawValue = cSsfs;
	}
}

View solution in original post

11 Replies

Avatar

Level 10

Hi,

 

you only need a few additional steps to accomplish this. Added rows can easily be moved by moveInstance().

 

var cInput = xfa.host.response("Enter number of rows you want to add:", "Add rows", "1", false),
	nIndex = this.parent.index,
	cDate = Date.rawValue,
	n = parseInt(cInput, 10) > 0 ? parseInt(cInput, 10) : 1,
	i = 0;
if (cInput !== null) {
	for (i; i < n; i += 1) {
		_Row1.addInstance(true);
		_Row1.moveInstance(_Row1.count - 1, nIndex + 1)
		this.parent.resolveNode('Row1[' + (nIndex + 1) + ']').Date.rawValue = cDate;
	}
}

Avatar

Level 6

Good night,

This is not working.  Now, it only adds a blank row underneath, even if I enter any other number than 1. The cancel button now works though.  The earlier scripts works except for that it changes the last row to whatever row you click on and it adds the row to the bottom. See below snips.  First snip is working.  I clicked the button and entered 3 and 3 rows were added, the 1 and date were also copied.  I changed the other rows to 2, 3, and 4,  The second snip, I clicked on the 2nd row button and entered only 1 because I want another 2 under the first 2 .  What happens is that Number 4 is being changed to 2 and an additional 2 is added to the bottom of table.

islandgirl23_0-1662339853767.png

islandgirl23_1-1662340124035.png

 

 

Avatar

Level 10

Looks like you've nested a subform or table in a row that contains the button. In that case the relative reference to the rows etc. are not correct. Please share your form or the at least a screenshot of the hierachry of the table and it's cells.

Avatar

Level 6

Thank you for responding.  Below is the hierachry of the table and it's cells.

islandgirl23_0-1662399102537.png

 

Avatar

Level 10

Should work this way. For me it does.

 

var cInput = xfa.host.response("Enter number of rows you want to add:", "Add rows", "1", false),
	oRow = this.parent.parent,
	nIndex = oRow.index,
	cDate = oRow.dated.rawValue;
	n = parseInt(cInput, 10) > 0 ? parseInt(cInput, 10) : 1,
	i = 0;
if (cInput !== null) {
	for (i; i < n; i += 1) {
		_Row1.addInstance(true);
		_Row1.moveInstance(_Row1.count - 1, nIndex + 1)
		oRow.resolveNode('Row1[' + (nIndex + 1) + ']').dated.rawValue = cDate;
	}
}

Avatar

Level 6

Thank you.  Before I check this, I noticed that there are no references to "ssfs".  Only "dated".  I will need "ssfs" to be copied/inserted as well.  Do I leave the "if" statement pertaining to "ssfs" or do I delete it or do I modify it?  Also, do I get rid of the first script?  "var nNewCardIndex = this.parent.parent.instanceManager.count-1;
var sTargetNew = "headersubform.tablesubform.tablesubforms.Table1.Row1[" + nNewCardIndex + "]";
this.resolveNode(sTargetNew).ssfs.rawValue = this.parent.parent.ssfs.rawValue;
this.resolveNode(sTargetNew).dated.rawValue = this.parent.parent.dated.rawValue;" or leave as is?

Avatar

Level 6

Radzmar,

This works perfectly for the "dated" column, but the information entered in the "ssfs" column is blank when the button is pressed. It's not copied. Can you assist with fixing that?

Avatar

Correct answer by
Level 10

What's the problem? You just have to to the same steps for ssfs as for dated.

 

var cInput = xfa.host.response("Enter number of rows you want to add:", "Add rows", "1", false),
	oRow = this.parent.parent,
	nIndex = oRow.index,
	cDate = oRow.dated.rawValue,
        cSsfs = oRow.ssfs.rawValue;
	n = parseInt(cInput, 10) > 0 ? parseInt(cInput, 10) : 1,
	i = 0;
if (cInput !== null) {
	for (i; i < n; i += 1) {
		_Row1.addInstance(true);
		_Row1.moveInstance(_Row1.count - 1, nIndex + 1)
		oRow.resolveNode('Row1[' + (nIndex + 1) + ']').dated.rawValue = cDate;
oRow.resolveNode('Row1[' + (nIndex + 1) + ']').ssfs.rawValue = cSsfs;
	}
}

Avatar

Level 6

Thank you for all of your help!  I was using cDate instead of something else.  It works perfectly now.  

Avatar

Community Advisor

Use this one, should work as per your requirement:

var cInput = xfa.host.response("Enter number of rows you want to add:", "Add rows", "1", false),
nIndex = this.parent.index,
cDate = date.rawValue,
n = parseInt(cInput, 10) > 0 ? parseInt(cInput, 10) : 1,
i = 0;
if (cInput !== null) {
for (i; i < n; i += 1) {
_Row1.insertInstance(nIndex+1,0);
this.parent.resolveNode('Row1[' + (nIndex + 1) + ']').date.rawValue = cDate;
}
}

Avatar

Level 6

Good day,

Thank you radzmar and VK85 for responding, but this is not working either so, to start from scratch, I posted all of the original scripts tied to the button below. I think this is the problem why both responses are not working. The issues are:

(1) if you click the "cancel" button pertaining to the question, it changes the entries of the last row to whatever is in the row that is clicked and also adds a row to the bottom of the table (the entries are also copied in that row): and

(2) if you click the button tied to a row, and enter a number to add rows, it changes the entries of the last row to the entries that is in the row that was clicked and also add the additional rows entered in the question block to the bottom of the table.  The entries are also copied. 

So, if I enter 4 in the question and I already have 5 rows, it will change the entries of row 5 to whatever entries that are in the row that was clicked and also add 4 additional rows of what was clicked.  Note:  If you enter "1" or leave "1" in the question box, it changes the last row of the table and adds an additional row as well.

Please help me to resolve this:

1. No rows are added and the last row/entries are NOT changed if I click the "cancel" button tied to the question.

2. When the button of a row is clicked, and the number of rows are entered, DO NOT change the last row of the table and DO NOT add the additional rows to the bottom of the table.  Instead "Insert" a row or rows directly BELOW the row click and also "copy" the entries in that row based upon the number entered in the question.  Hope this helps.  Also, if you can tell me what the scripts are doing, it will teach me and it may also help solve the problem.

 

var nNewCardIndex = this.parent.parent.instanceManager.count-1;
var sTargetNew = "headersubform.tablesubform.tablesubforms.Table1.Row1[" + nNewCardIndex + "]";
this.resolveNode(sTargetNew).ssfs.rawValue = this.parent.parent.ssfs.rawValue;
this.resolveNode(sTargetNew).dated.rawValue = this.parent.parent.dated.rawValue;

var cInput = xfa.host.response("Enter number of rows you want to add:", "Add rows", "1", false),
n = parseInt(cInput, 10) > 0 ? parseInt(cInput, 10) : 1,
i = 0;
for (i; i < n; i += 1) {
_Row1.addInstance(true);
}

if (!dated.isNull) {
var oRows = Table1.resolveNodes('Row1[*]'),
j = this.parent.index,
cDate = dated.rawValue;
for (j; j < oRows.length; j += 1) {
if (oRows.item(j).dated.isNull) {
oRows.item(j).dated.rawValue = cDate;
}
}
}

if (!ssfs.isNull) {
var oRows = Table1.resolveNodes('Row1[*]'),
j = this.parent.index,
cDate = ssfs.rawValue;
for (j; j < oRows.length; j += 1) {
if (oRows.item(j).ssfs.isNull) {
oRows.item(j).ssfs.rawValue = cDate;
}
}
}