Expand my Community achievements bar.

SOLVED

Prevent Identical Selection in Drop Down List in an Expanding Table

Avatar

Former Community Member

Hi All,

I have a 2x2 table in which the top row is a header row and the bottom row 'Row1'. Row1 Cell1 has two buttons in it, one to add a new Row1 using Javascript click instance script:

this.parent.parent.instanceManager.addInstance()

...and one to remove any Row1 using Javascript click instance script:

var rowNum = this.parent.parent.index;

this.parent.parent.instanceManager.removeInstance(rowNum);

Row1 Cell2 contains a datadropdown object.  Basically, when a user adds an extra Row1, I need to prevent them from selecting a previously selected item from the dropdowndata list.  I'm having trouble figuring it out because I'm trying to put together exit instance script (or should it be validate script?) that is trying to reference Row1[*].  In simpler situations where I have two fixed objects, I have just been doing this as a FormCalc exit script:

if (Object1 = Object2)

then xfa.host.messageBox("You can't select the same thing again")

and xfa.host.resetData("Object1")

endif

... which prevents the user from selecting something with the same rawvalue by alerting them, then clearing the field. 

Hope this makes sense and that someone can help!  Thanks in advance to all the adobe gurus out there

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

Would it be possible to filter out the choices in the drop down list that have already been selected.  If so you could try something like this JavaScript in the preOpen event of your drop down.

var dropDownItems = [ "abc", "def", "hij" ];

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

for (var i = 0; i < rows.length; i++)

{

    var row = rows.item(i);

    if (!row.DropDownList1.isNull)

    {

        dropDownItems = dropDownItems.filter(function(element) { return element !== row.DropDownList1.rawValue});

    }

}

this.setItems(dropDownItems.join(","));

The first line initialises a variable with all the possible values and then we go though each row filtering out the ones already selected. 

You will probably have to change the form object names to suit you form, but here is my sample to test the code, https://files.acrobat.com/preview/eb0256dc-af30-4f7c-9187-469ba84464a4

Regards

Bruce

View solution in original post

3 Replies

Avatar

Correct answer by
Level 10

Hi,

Would it be possible to filter out the choices in the drop down list that have already been selected.  If so you could try something like this JavaScript in the preOpen event of your drop down.

var dropDownItems = [ "abc", "def", "hij" ];

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

for (var i = 0; i < rows.length; i++)

{

    var row = rows.item(i);

    if (!row.DropDownList1.isNull)

    {

        dropDownItems = dropDownItems.filter(function(element) { return element !== row.DropDownList1.rawValue});

    }

}

this.setItems(dropDownItems.join(","));

The first line initialises a variable with all the possible values and then we go though each row filtering out the ones already selected. 

You will probably have to change the form object names to suit you form, but here is my sample to test the code, https://files.acrobat.com/preview/eb0256dc-af30-4f7c-9187-469ba84464a4

Regards

Bruce

Avatar

Former Community Member

Hi Bruce,

I haven't had a chance to apply the script to my own example (its at work) but have checked out your sample file and it seems to work perfectly - thank you for taking the time to provide the very elegant solution   I only have one concern though: you've used the first line to initialise a variable with all possible values, and in my situation some of the lists are hundreds of items long, and many characters in length.  To date, I've been cut and pasting out the catalogue of 'codeset' values from word/excel lists into the LCD (Drop Down Data List) Object>Cell>Cell Items field, which has saved me typing out enormous lists of information.  I must confess that I've got no programming experience and have been using JavaScript for exactly two days, so I'm still not sure how to turn a list of information into a data source for my form to map to - is this the direction I need to be heading in?  To give you some context, the forms I am creating contain large lists of medical and/or health services information; ideally what I need are forms with codesets that a lay person could update from time to time, by manipulating the data in a common file format and application (like excel).  I really need the codeset data to map to an actual  file too, as I have limited scope for implementing and managing a webservice or proper database solution.  Any advice or instruction on how I could accomplish any of this would be truly appreciated!

Kind Regards,

Tanzil

Avatar

Level 10

Hi Tanzil,

You can keep you current approach of having the values defined at design time and replace the first line with something like;

var dropDownItems = [];

var templateDropDownList = xfa.template.form1.Table1.Row1.DropDownList1;

for (var i = 0; i < templateDropDownList.items.nodes.length; i++)

{

    dropDownItems.push(templateDropDownList.items.nodes.item(i).value);

}

This picks up the values from the template DOM, so I'm using xfa.template on the second line were you usually see xfa.form. 

I would be interested to hear how responsive this is once you get a few hundred records in the list.

For your ongoing updates to the codesets it would depend on what you are going to do with the forms.  Does the data get extracted from them when they are sent in?  If not then the dropdown list can be bound to the xml data source and if someone has Acrobat they can import a new xml data source.  If the data is being extracted then you probably don't wont to extract all the codeset data at the same time.  You can deal with multiple datasets and there is an example on John Brinkman's blog, http://blogs.adobe.com/formfeed/2009/11/working_with_multiple_datasets.html

Good luck

Bruce