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.

Multiselect in a list box

Avatar

Former Community Member
I have a list box where I've set it to be multiselect (ie. this.ui.choiceList.open="multiSelect"). When a user selects one or more values I want to get the selected choices. I want to be able to determine the selected text and the bound value.



I've got this working for a single selection by using the following code in the change event for the ListBox1.



var itemText=xfa.event.newText;

var itemId=this.boundItem(xfa.event.change);



When I try to select more than one row I only see the last item selected.
20 Replies

Avatar

Former Community Member
When you retrieve the rawValue of the list box when it contains a multi-selection, the selected values will be separated by a new line (\n) character.



You could use the current (or old) rawValue of the list along with the value of the xfa.event.modifier (control key) and xfa.event.shift (shift key) properties to determine if the rawValue will be replaced (neither control nor shift were pressed at the time the Change event fired) -- in which case you can use the code you already have working for a single selection -- or if items (values) will be added/removed to/from the selection.



Stefan

Adobe Systems

Avatar

Former Community Member
The rawValue of the list box always appears to be blank.



To try to access the value I have the following code in the change event for the list box.



var selectedItemsStr=this.rawValue;

var selectedItems=selectedItemsStr.split('\n');

app.alert("selectedItemsStr="+selectedItemsStr);

app.alert("num selectedItems="+selectedItems.length);



I also tried putting the full name of the list box node rather than just using 'this'. Do I have to do something like this.parent.rawValue???

Avatar

Former Community Member
No, you don't need to do "this.parent.rawValue". As long as this code is on an event on the list box, "this.rawValue" is all you should need. Make sure the script language is
JavaScript.



Does the list box have an initial selection when the form is loaded (after items are added to it)? If not, it would explain the "always empty" rawValue.



You can set the initial value right after adding the items to the list box by assigning the values pertaining to the items to the rawValue. For example, if you add an item as follows:



this.addItem("text", "value");


You can set it as the initial selection as follows:



this.rawValue = "value";


Stefan

Adobe Systems

Avatar

Former Community Member
Did everything you said and it's still not working. Is there some fundamental document option that I may not have set to get this to work? I have a dynamic form and the language is Javascript. Anything else I may have missed?

That rawValue never changes no matter what I select. When I output the XML using saveXML I see a node


id1


This value doesn't change when I select other rows. Would it help to pass you an simple PDF to look at?

Avatar

Former Community Member
Here's the sample : http://www.samples.nancywright.ca/Adobe/SampleMultiselectListbox.pdf



It works perfectly when I run it in the IE plug-in for Adobe but doesn't work when I run it in Designer (evaluation version) or in Adobe Reader directly! What am I missing?

Avatar

Former Community Member
It might be an Adobe Reader version issue. Doesn't work on Adobe Reader 7.0.0 but seems to work on Adobe Reader 7.0.7. I'll try to confirm...

Avatar

Former Community Member
I'll have a look at the form. In the mean time, I've been hammering-out a solution. I'm still working on it when I get a free minute here and there. It turns-out I'm seeing some similar issues with the rawValue not being current. Multi-selection list boxes are a little more trickier than single-selection list boxes.



I'll post my solution ASAP.



Stefan

Adobe Systems

Avatar

Former Community Member
Got it. Had to do a this.saveXML() and then afterwards the this.rawValue() was correct! Boy, that was a tough one. Works fine now.



Thanks for the help.

Avatar

Former Community Member
Richard, you've taught me something today. That was definitely a tough one!



So for those of you landing on this thread looking for answers, we were trying to figure-out how come the rawValue of a list box never changes in the list box's Change event such that we can get the new selection in the list box.



Richard figured-out that if you do a saveXML call, it'll force the value to update so that rawValue will finally return the new selection.



So instead of simply doing:



var sNewSelectedValues = this.rawValue;"


You need to do add the saveXML call first:



this.saveXML();

var sNewSelectedValues = this.rawValue;"


At this point, the underlying XML will be updated and "this.rawValue" will return, in a \n-delimited string, the new values selected in the list box.



Thanks Richard!



Stefan

Adobe Systems

Avatar

Former Community Member
FormBuilder@adobeforums.com wrote:

> Richard, you've taught me something today. That was definitely a tough one!

>

> So for those of you landing on this thread looking for answers, we were trying to figure-out how come the rawValue of a list box never changes in the list box's Change event such that we can get the new selection in the list box.

>

> Richard figured-out that if you do a saveXML call, it'll force the value to update so that rawValue will finally return the new selection.

>

> So instead of simply doing:

>

> var sNewSelectedValues = this.rawValue;"

>

> You need to do add the saveXML call first:

>

> this.saveXML();

> var sNewSelectedValues = this.rawValue;"

>

> At this point, the underlying XML will be updated and "this.rawValue" will return, in a \n-delimited string, the new values selected in the list box.

>

> Thanks Richard!

>

> Stefan

> Adobe Systems



This is not the appropriate way to do this. You should not have to call

saveXML() to get the value. When you are working with the change event

for a list box, the rawValue does not give you the new value, it always

gives you the old value (as you both have experienced). In order to

access the old and new values of the drop-down list in the change event,

you should use the following syntax:

xfa.event.prevText

xfa.event.newText



For any other event besides 'change', you can access the value of the

drop-down list by using the rawValue property.



Hope this helps!



Justin Klei

Cardinal Solutions Group

www.cardinalsolutions.com

Avatar

Former Community Member
Do you think it's worth me posting a sample PDF of this? Where can I post samples?

Avatar

Former Community Member
Actually it was a combination of this.saveXML() and I had to upgrade the version of Adobe Reader to 7.0.7. When I was using Adobe Reader 7.0.0 it didn't work. Strange...

Avatar

Former Community Member
I believe you need special permission to post samples. I would just post the Initialize and Change event code.



Stefan

Adobe Systems

Avatar

Former Community Member
Note that this will only work if the text and value assigned to an item in the list box are the same as well as only work in the single-selection case.



xfa.event.newText will only be set to the
item text of the item which was clicked by the mouse. If an item's value differs from its text (e.g. you have an item named "Vegetables" with a value of "1"), xfa.event.newText will be set to "Vegetables", not "1". Although you can determine that the value of the new selected item is "1" by using this.boundItem(xfa.event.newText), you can't determine the value of
all items that were selected.



In a multi-select list box, simply using xfa.event.newText won't work in all cases because even when the user shift-clicks an item to select all items from the last selected item to the item clicked, xfa.event.newText will still only be set to the item that was clicked as opposed to being, for example, a newLine-delimited string of all items that were selected as a result of the shift-click.



Although we shouldn't have to be calling this.saveXML() prior to retrieving this.rawValue, it's the only way we've found to successfully retrieve the value of
all items that were selected at the time of the Change event.



Stefan

Adobe Systems

Avatar

Former Community Member
FormBuilder@adobeforums.com wrote:

> Note that this will only work if the text and value assigned to an item in the list box are the same as well as only work in the single-selection case.

>

> xfa.event.newText will only be set to the item text of the item which was clicked by the mouse. If an item's value differs from its text (e.g. you have an item named "Vegetables" with a value of "1"), xfa.event.newText will be set to "Vegetables", not "1". Although you can determine that the value of the new selected item is "1" by using this.boundItem(xfa.event.newText), you can't determine the value of all items that were selected.

>

> In a multi-select list box, simply using xfa.event.newText won't work in all cases because even when the user shift-clicks an item to select all items from the last selected item to the item clicked, xfa.event.newText will still only be set to the item that was clicked as opposed to being, for example, a newLine-delimited string of all items that were selected as a result of the shift-click.

>

> Although we shouldn't have to be calling this.saveXML() prior to retrieving this.rawValue, it's the only way we've found to successfully retrieve the value of all items that were selected at the time of the Change event.

>

> Stefan

> Adobe Systems



Sorry, I misunderstood the problem. I think the real issue here is that

multiselect list boxes are not fully supported. Be careful when using

these. If you try to export xml data from the form, you will get mixed

results. Sometimes it exports all of the values, sometimes it exports

one of the values, and sometimes it exports some of the values...none of

which seem to match what was actually selected. At least this is what I

experienced in limited testing. Proceed with caution!!!



Justin Klei

Cardinal Solutions Group

www.cardinalsolutions.com

Avatar

Former Community Member
I found that upgrading to Adobe Reader 7.0.7 fixed a lot of the issues I was having with multi-select list boxes.

Avatar

Former Community Member
I did a little digging on our side of things here and I unfortunately have to conclude that there currently (up to and including Acrobat 7.0.7) isn't a proper way to retrieve the values of items that have been selected in a list box at the time of the Change event.



The fact that calling saveXML() on the list box prior to retrieving it's rawValue updates the rawValue to the selected values isn't the intended way for things to work. As such, I'm offering a warning that
this solution may not work in future versions of Acrobat.



Therefore, if you're needing to know which values have been selected in a multi-select list box at the time of the Change event, the easiest solution is to use the saveXML() method however you should probably verify that the version of Acrobat you're using will do what's expected. You can retrieve the version of Acrobat the form is being opened in by using the following code:



xfa.host.version


This will return a string representing the version (e.g. the string returned in Acrobat 7.0.5 is "7.05").



Stefan

Adobe Systems

Avatar

Former Community Member
Just curious but why does it say "7.05" when in fact I am using version 7.0.7?

Avatar

Former Community Member
That's either because you've saved (or are previewing) the form as an Acrobat 7.0.5 PDF file or it's a bug in Acrobat. You could also try



app.viewerVersion


to see if this returns "7.07" in Acrobat 7.0.7.



Stefan

Adobe Systems

Avatar

Former Community Member
It doesn't.. it's a known bug in Acrobat 7.0.7. If you really need a way to determine if your in 7.0.7 in script you can query the Forms plugin in the plugins object, here's an example.



Chris

Adobe Enterprise Developer Support