Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Sorting a picklist

Avatar

Level 3

I have a pull down box populated by a case statement. I would like to sort that list alphanumerically "on the fly" if that is possible. Is there some kind of "sort" script that will do this?

Thanks

-pc

1 Accepted Solution

Avatar

Correct answer by
Level 10

Ok,

in this case you can use a script in the enter:Event of the drop down box which will extract all current records, sort them and write them back into the box.

var ListItems = new Array();


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

{

var nItem = this.getSaveItem(i);

    ListItems.splice(i, i, nItem);

    ListItems.sort();

}

this.clearItems();


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

{

this.addItem(ListItems[i]);

}

Hope this helps

View solution in original post

6 Replies

Avatar

Level 10

Hi,

generally "yes". You can use the sort() function of javascript.

How do you populate your drop down box?

Avatar

Level 3

Using a case statement like - but I have several of these tied to on dropdownlist......

case

"NW1 - WA":

for (var i=1; i<=7; i++)

{

var DD = xfa.resolveNode("form1.Page1.Osr"+i);

DD.rawValue

= null;

DD.clearItems();

DD.addItem("Gary Gray");

DD.addItem("Mike Davis");

DD.addItem("James Schiessl");

DD.addItem("Joel Schalo");

DD.addItem("Todd Olson");

DD.addItem("Ken Canady");

DD.addItem("Tony Adams");

DD.addItem("April Gray");

DD.addItem("Melanie Smith");

DD.addItem("Elisa Tamez");

DD.addItem("James Moore");

DD.addItem("Chrissy Cope");

DD.addItem("Gregg Petet");

DD.addItem("Chris Danhof");

DD.addItem("Chuck Baker");

DD.addItem("Michelle Russell");

DD.addItem("Mingie Holland");

}

break;

Avatar

Correct answer by
Level 10

Ok,

in this case you can use a script in the enter:Event of the drop down box which will extract all current records, sort them and write them back into the box.

var ListItems = new Array();


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

{

var nItem = this.getSaveItem(i);

    ListItems.splice(i, i, nItem);

    ListItems.sort();

}

this.clearItems();


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

{

this.addItem(ListItems[i]);

}

Hope this helps

Avatar

Level 3

So where would I actually drop in that code?

I have my "Region" pull down that populated the "District" pulldown that populates the "Location" pulldown that populates the "Account Lead" pulldown.

The accound lead are the items I want to sort alphabetically. I added the code both to the Location and Account Lead and it didn't seem to sort.

Dumb question - do I need to make changes to the script you posted to make it work with my specific form?

Thanks,

-pc

Avatar

Level 3

Added a -1 to the length statement

for

(var i=0; i < this.length-1; i++)

and it works!

Thanks,

Patrick

Avatar

Level 3

Actually it works exactly as you posted....when you add the -1 it removes a name from the list each time you open it which is kind of interesting but probably not usefull....Anyway Thanks!