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.
SOLVED

Copy Table Enteries to One Text Field

Avatar

Level 5

The user of the form I am desiging will enter text into a two column table. The user has the ability to create additional text boxes in the table to contain more names. I have written a javascript to take the names from each box and place all of the names into one text field seperated by a semicolon at the bottom of the graphic. Well that's the idea at least. Here is a graphic of what I would like the "Copy All" button script to do.

Display.jpg

//Javascript for "Copy All" Button

var mouse = new Array();

var loopCounter;

for (loopCounter = 1; loopCounter == null; loopCounter++)

{

   mouse[loopCounter].rawValue = xfa.resolveNode("form1.Table1.Row1.TextField[loopCounter]") + ";";

}

Address.rawValue = mouse.rawValue;

The code returns "Empty" into the bottom text field where I would like all the names to go. Any errors spotted or ideas about how to better accomplish this task? I don't understand how to get the "Address" field to equal all the results of an array. Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Ah ok, so you to test for null.

for (i = 0; i < rowCount; i++) {

          if (Address.rawValue == null) {

                    Address.rawValue = rows.item(i).TextField1.rawValue + ";";

                    Address.rawValue += rows.item(i).TextField2.rawValue + ";";

          }

          else {

                    Address.rawValue += rows.item(i).TextField1.rawValue + ";";

                    Address.rawValue += rows.item(i).TextField2.rawValue + ";";

          }

}

View solution in original post

6 Replies

Avatar

Level 10

You've got a few things going on...you need something for the loop to count (number of rows) and arrays don't use .rawValue (you need to use push to add values). And you're putting the loopcounter variable in the wrong spot, it should be on Row1 not the textfield.

It might be easiest to keep the array out of it and just copy the data to the text field.

Something like:

var rows = resolveNodes("Table1.Row1[*]"); //count the rows

var rowCount = rows.length; //assign row count to variable

for (i = 0; i < rowCount; i++) {

          Address.rawValue += rows.item(i).TextField1.rawValue + ";"; //add values to text field

          Address.rawValue += rows.item(i).TextField2.rawValue + ";"; //add in the other text field

}

Avatar

Level 5

This scirpt works great. However the result in the "Address" field returns the names entered starting with null. For instance:

nullBiltrey, James E. ; Tigert, Williams S. ; ......................

null is only given one time however at the begining of the list. I need a way to give the "Address" field a value of empty but not null, before adding the first row.item(0)TextField1.rawValue to it.

Avatar

Correct answer by
Level 10

Ah ok, so you to test for null.

for (i = 0; i < rowCount; i++) {

          if (Address.rawValue == null) {

                    Address.rawValue = rows.item(i).TextField1.rawValue + ";";

                    Address.rawValue += rows.item(i).TextField2.rawValue + ";";

          }

          else {

                    Address.rawValue += rows.item(i).TextField1.rawValue + ";";

                    Address.rawValue += rows.item(i).TextField2.rawValue + ";";

          }

}

Avatar

Level 5

This code works great, but if the last row has an empty cell I get a list of email addresses with a null at the end of the list. See Figure 1

FIGURE 1Emails with Null Field.JPG

I re-wrote the code as a series of if else statements to test for an null value and move on if a null value was found. However I believe my "Email List" field is loosing the combined values from all the if then statements I used. Thus only leaving me with the result of the last cell run through the code. In the example below (FIGURE 2) that is "Smith, Tom". How do I test for null and move on if found?

FIGURE 2Emails Screenshot.JPG

var rows = resolveNodes("Table1.Row1[*]"); //count the rows

var rowCount = rows.length; //assign row count to variable

for (i = 0; i < rowCount; i++)

{

if (form1.Address.rawValue == null)

   {

                     if(rows.item(i).TextField1.rawValue != null)

                     {

                     form1.Address.rawValue = rows.item(i).TextField1.rawValue + "; ";

                     }

                     else

                     {};

                     if(rows.item(i).TextField2.rawValue != null)

                     {

                     form1.Address.rawValue = rows.item(i).TextField2.rawValue + "; ";

                     }

                     else

                     {};

   }                    

else

       {

                     if(rows.item(i).TextField1.rawValue != null)

                     {

                     form1.Address.rawValue = rows.item(i).TextField1.rawValue + "; ";

                     }

                     else

                     {};

                     if(rows.item(i).TextField2.rawValue != null)

                     {

                     form1.Address.rawValue = rows.item(i).TextField2.rawValue + "; ";

                     }

                     else

                     {};

        }                    

}

;

Avatar

Level 10

You need to test each rawValue for null before adding it. The following does the trick though there is probably a more elegant way of doing it.

You'll need to poke your own field names in. I changed the script a bit from before and assigned variables for the text fields.

var rows = resolveNodes("Table1.Row1[*]");

var rowCount = rows.length;

Address.rawValue = null;

 

for (i = 0; i < rowCount; i++) {

          var field1 = rows.item(i).TextField1.rawValue;

          var field2 = rows.item(i).TextField2.rawValue;

           if (Address.rawValue == null) {

                    if (field1 != null) {

                              Address.rawValue = field1 + ";";

                    }

                    if (field2 != null) {

                              Address.rawValue += field2 + ";";

                    }

          }

          else {

                    if (field1 != null) {

                              Address.rawValue += field1 + ";";

                    }

                    if (field2 != null) {

                              Address.rawValue += field2 + ";";

                    }

          }

}

Avatar

Level 5

Works like a charm. Thanks again Jono!