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.

List box Alphabetical Order

Avatar

Level 2

Hi all,

How would you order a list of entries in an alphabetical way.

Let's say you have a Drop Down List with Custom Entry enabled. I would like to add the new entry alphabetically in the new list. How would I do that?

My specific problem is that i have two list boxes. If you add something to one side, it gets deleted from the other side and vice versa.

However it is always added at the end of the list:

Here my code:

BusinessesSupported.addItem(ListBox0.getDisplayItem(ListBox0.selectedIndex))

var

newList = new Array();

var

nItems = ListBox0.resolveNodes("#items.#text[*]");

var

count = nItems.length;

var

listCount = 0;

for

(var i = 0; i < count; i++) {

if

(nItems.item(i).value != ListBox0.rawValue) {

newList[listCount]

= nItems.item(i).value;

listCount

++;

}

}

ListBox0.clearItems();

for

(var i = 0; i < listCount; i++) {

ListBox0.addItem(newList[i]);

}

Any ideas?

Thanks,

Larissa

10 Replies

Avatar

Level 10

You need to take all the listbox items and create an array with that values. Then you can sort Array and then reload the list box in the order the array has.

I am placing a sample javascript code here. You may modify according to your need..

var fruits = ["Banana", "Orange", "Apple", "Mango"]; // Here I am hardcoding the values to the array. You may need to populate this array with the values from listbox.

fruits.sort();

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

}

Thanks

Srini

     xfa.host.messageBox("" + fruits[i]);

Avatar

Level 2

Hi Srini,

As my objects/items in the list box are changing, how would I do that?

Thanks,

Larissa

Avatar

Level 10

I have a made simple form and is available for your use at the below link.

https://acrobat.com/#d=Lvypaju3TYT4Qz7V*eThDg

I have placed two listboxes on the form (ListBox1 and ListBox2). ListBox1 has data in it and ListBox2 is empty. At runtime, you can select a value form ListBox1 and click on "Add" button to add the selected item to ListBox2.

Placed the below code in the Click event of the "Add" button.

//do the below processing only if the item is selected in the ListBox1
if(ListBox1.selectedIndex>=0){
ListBox2.addItem(ListBox1.rawValue);

var oStrArray = new Array();

//Loop thru the Listbox and populate the array
for(var i=0;i<ListBox2.length;i++){
  oStrArray[i] = ListBox2.getDisplayItem(i);
}

//sort the Array
oStrArray.sort();

//Clear the Listbox
ListBox2.clearItems();

//Loop thru the array to populate the List box with the sorted data
for(var j=0;j<oStrArray.length;j++){
  ListBox2.addItem(oStrArray[j]);
}
}

Thanks

Srini

Avatar

Level 2

Hi Srini,

Tried it out but it does not work. Don't know why.

Here my code:

I have put it on the enter event of the list box.

var newList = new Array();

var nItems = BusinessesSupported.resolveNodes("#items.#text[*]");

var count = nItems.length;

var listCount = 0;

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

newList[i] = BusinessessSupported.getDisplayItem(i);}

newList.sort();

BusinessesSupported.clearItems();

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

BusinessesSupported.addItem(newList[i]);

}

The last entry adds at the bottom and not in alphabetical order.

Also for some reason, I cannot access your file.

Thanks for the help.

Larissa

Avatar

Level 10

I think instead of enter event of ListBox2, try putting the code in the Click event of the Add button which will copy the selected value from ListBox 1 to ListBox2.

When you change the code to a different event, you need to change the "this.length" to "count"  in this location of code."for (var i = 0; i < this.length; i++) {"

I tried with your code and it is working in sample. I put the code in the click event of the add button.

Let me know if this helps.

Thanks

Srini

Avatar

Level 2

Hi Srini,

Still no luck.

I have tried what you said but the item keeps adding at the bottom. I don't understand.

Here my code:

BusinessesSupported.addItem(ListBox0.getDisplayItem(ListBox0.selectedIndex))

var newList = new Array();

var nItems = ListBox0.resolveNodes("#items.#text[*]");

var count = nItems.length;

var listCount = 0;

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

if (nItems.item(i).value != ListBox0.rawValue) {

newList[listCount] = nItems.item(i).value;

listCount++;

}

}

newList.sort()

ListBox0.clearItems();

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

ListBox0.addItem(newList[i]);

}

I 've put it on the Click Event of the Button.

Thanks,

Larussa

Avatar

Level 10

Can you upload your document..

Steps to upload the document

1) goto acrobat.com website
2) login with your Adobe login/password.
3) In Actions you will find Upload. Select your form and upload.
4) After uploading, select form name in All Files section and right click and select share.
5) Check the checkbox "Allow anyone with a link to view this document".
6) Copy the URL link and post it in the forum.

Thanks

Srini

Avatar

Level 2

Hi Srini,

Have uploaded the file.

https://acrobat.com/#d=dd0bhEOBhDTa9M4760cLOg

Thanks for taking a look.

Larissa

Avatar

Level 10

Updated file is available at the below link..

https://acrobat.com/#d=Wuvn0O-iaFVqtaCbs910pQ

Hope this helps.

Thanks

Srini

Avatar

Level 2

Hi Srini,

Thanks for that but the entries selected in Box 1 have to disappear in order to avoid double entry.

My code did that however did not sort the entries when you click on the remove Button to go from Box 2 back to Box 1.

Just try on my pdf to add an item from left to right with the add button. The Entry disappears on the left and appears on the right.

Then click on the Remove Button, the entry disappears on the left and appears on the right but at the bottom of the list thus not sorted.

I would need the Sorted function on the Remove Button. However, I've tried adding it and it just does not have any effect.

Thanks a lot.

Larissa