Expand my Community achievements bar.

Creating drop down list from values in table

Avatar

Former Community Member

I wonder if someone can help me.

I have a table where users will enter values into a text field in one of the columns.  Users will be able to add or delete rows in the table.  I want to use the values that are entered into the cells in that column to create a drop down list in a cell in another table.  I've been able to pull values from text fields into text fields in other tables before but I'm not sure how to go about creating a drop down list.

Any help much appreciated.

9 Replies

Avatar

Former Community Member

You woudl create your empty Dropdown list (lets call it DDList). Then when you want to add something to it you woudl use DDList.addItem(Item to Add). So if you have Field1 that you want to add to the list you woudl use: DDList.addItem(Field1.rawValue)

Paul

Avatar

Level 2

Is there anyway of using script to access that value list? I populate a DDL in a dynamic table row using additem, but when I add a new row using script that duplicates the previous row, the DDL values aren't duplicated. So, somehow I need to recreate the value list in the new DDL in the new row.

Avatar

Level 10

You can create a script object which will populate the Dropdown by using the addItem method.

Then in the Initialize event of the dropdown call this function to populate the field.

That way when you create a new row, the initialize event will be called and the dropdown will be populated.

Thanks

Srini

Avatar

Level 2

Thanks for the suggestion, how do I reference the value list of another DDL object in order to duplicate its value list? I know how to use addItem(); just not sure how to access (or reference) the other DDL's value list to loop through to get it to populate the new DDL with the same value list.

Avatar

Former Community Member

I'm struggling to get this to work correctly.  My knowledge of Javascript is somewhat limited..

I have put the following script under both the Exit and Initialize event in the text field of the first table:

Registered.Details.Table.Row1.Entity.addItem(Entities.Table.Row1.LegalEntity.rawValue)

It works fine if I only have one row in both tables but as soon as I add another row to the first table the drop down list contains 3 items all of the value from the first row in the first table rather than 1 of the value from the first row and 1 of the value of the second row.  Basically I need to get the values from everything in column 1 of this table into the drop down list without knowing how many rows there are likely to be.

I also have the problem in that I need to duplicate the drop down list in the new rows that may be added to the second table.  I've tried entering the same script in the Initialize event of the drop down list but that's not working.  I've also tried adding it to the click event of the button to add new rows and I can't get it to work that way either.

I'm probably entering the scripts in all the wrong places - where should they go?

Also if someone deleted a row from the first table would the value in the drop down list also be deleted?

Avatar

Level 2

Hi,

If you can send me your example I can take a look and see if I can work it out.

Kind regards,

David

Avatar

Former Community Member

Thank you for your offer of help.  As attaching documents has been disabled would it be ok for me to email it to you?

Avatar

Former Community Member

Finally managed to get this to work with David's help.  Solution if anyone else needs it:

Create a function - Form1.formScripts:

function populateColumn4(){

// get the numbers of rows in both tables.

var srcRow = form1.Top.Questions.Table1.Table1.Details.Row1;

var srcRowCount = srcRow.instanceManager.count;

var destRow = form1.Top.Questions.Table1.Table2.Details.Table.Row1;

var destRowCount = destRow.instanceManager.count;

// cycle through the dropdown in each of the destination rows and empty their list.

for (var j=0; j < destRowCount; j++) {

xfa.resolveNode("form1.Top.Questions.Table1.Table2.Details.Table.Row1[" + j + "].Column4").clearItems();

// Now fill the drop down on the current destination row with each of the values in the source row.

for (var i=0; i < srcRowCount; i++) {

var fieldValue = xfa.resolveNode("form1.Top.Questions.Table1.Table1.Details.Row1[" + i + "].Column1").rawValue;

xfa.resolveNode("form1.Top.Questions.Table1.Table2.Details.Table.Row1[" + j + "].Column4").addItem(fieldValue);

}

}

}

Call the function formScripts.populateColumn4(); in the following:
(a) Exit of column1 of the first table
(b) Mouseenter of column4 (where your drop-down list is located) on the second table
(c) Click event on the add row button on the second table
In the Click event of the delete row button of the first table:
// get the number of rows to check if they have instances of the to-be-deleted Data.
var destRow = form1.Top.Questions.Table1.Table2.Details.Table.Row1;
var destRowCount = destRow.instanceManager.count;
// get the index of the row to be deleted.
var delIndex = parent.parent.index;
// get the to-be-deleted data value for comparision
var delValue = xfa.resolveNode("form1.Top.Questions.Table1.Table1.Details.Row1 [" + delIndex + " ].Column1").rawValue;
// loop through the 'destination' rows looking for the same data.
for(i=0; i< destRowCount; i++){
var destObject =  xfa.resolveNode("form1.Top.Questions.Table1.Table2.Details.Table.Row1[" + i + "].Column4");
var destValue = destObject.rawValue;
// if the deleted data and destination data are the same, then set the destination data to 'null'.
if(delValue == destValue){
// (you could delete the row altogether at this point if you wanted by using the removeInstance method.)
destObject.rawValue = null;
}
}
// remove the source data row.
parent.parent.instanceManager.removeInstance(parent.parent.index);
}
}