Stefan Cameron on Forms
Building intelligent forms using Adobe LiveCycle Designer

Sorting Lists at Runtime

Have you ever needed to sort the content of a list box or drop down list at runtime (e.g. in a form loaded in PDF with Acrobat or HTML with a browser)?

Unfortunately, neither the XFA nor the AcroForm Object Models give you either properties or methods to achieve this functionality. The only thing available are Sort Ascending and Sort Descending buttons on the list of items you define in the Object palette’s Field tab in Designer — not very useful at runtime! Say you were loading data from an XML Data File into your form and this was populating a list with values that needed to be sorted: There wouldn’t be a built-in way for you to do that.

The only solution I’m aware of at this time is to flex your scripting muscles and write some functions that’ll get you sorting lists (from either list boxes or drop down lists) and that’s exactly what I did today while replying to a post on Adobe’s Designer Forums:

Download Sample [pdf]

Minimum Requirements: Designer 7.0, Acrobat 7.0.

Update: It looks like Acrobat 8.0 broke something that prevents my sample form from working correctly. It has to do with the call to

resolveNodes("#items[*]")

Fortunately, the bug will be fixed in Acrobat’s next release.

As a workaround, I would encourage you to have a look at the new list object properties and methods now available in Acrobat 8.0. You should be able to use a combination of those new properties/methods along with some of the original script in this sample in order to come-up with an update solution that works.

All the code is located in the script object appropriately named, “ScriptObject”.

The idea was to make use of the JavaScript Array object’s built-in sorting function, sort, by giving it a custom sorting function that was able to sort pairs of text and value items. Remember that for XFA choiceList fields, there’s always one <items save=”1”> node but there may be a second <items> node if text and value items are defined.

From Designer’s UI, you define value items by default by entering items in the list on the Object palette’s Field tab. This defines an <items save=”1”> node. If you then go to the Bindings tab and specify values, the items you specified on the Field tab become the “text” items, defined in the second <items> node, and the values are set in the <items save=”1”> node.

When sorting the list, it’s important to maintain the association between text and value item pairs and that’s why the script is a little complex — not to mention the fact that getting items out of an XFA choiceList field (list box or drop down list) isn’t as easy as 1-2-3 either!

So the script attempts to find text and/or value items, create ListItem objects for each pair, sort them using the custom sort function __SortFunction and then replaces the current items in the list with the ones in the sorted order.

Updated: April 11, 2007


Posted by Stefan Cameron on June 29th, 2006
Filed under Scripting,Tutorials
Both comments and pings are currently closed.

12 Responses to “Sorting Lists at Runtime”

  1. Rebecca Lewis on July 15th, 2006

    I Love you Stefan – Happy 4th Anniversary 🙂

  2. Stefan Cameron on July 16th, 2006

    Dear Rebecca (my amazing wife),

    I love you too. I thank God for 4 wonderful years with you so far and I look forward to many more!

  3. Arne Metzner on March 20th, 2007

    When I open the dokument I have no function. Can you send me a mail how I could use this PDF.

    With best regards

    Arne Metzner

  4. Stefan Cameron on March 21st, 2007

    Arne,

    You should use Designer 7.0 or later to open this sample PDF form.

    Opening it in Acrobat will only let you use the form to see how it works however you won’t be able to see the script that makes it work — you’ll need Designer for that.

  5. Christian on April 10th, 2007

    Hi,

    sorry but there is in Arcobat 8.0 no functionality. So if you click on sort nothing happens. Is it like this, that something has changed between 7.0 and 8.0 ?

  6. Stefan Cameron on April 11th, 2007

    Christian,

    Thanks for pointing this out. It looks like Acrobat 8.0 broke something that prevents my sample form from working correctly.

    It has to do with the call to

    resolveNodes(“#items[*]”)

    Fortunately, the bug will be fixed in Acrobat’s next release.

    As a workaround, I would encourage you to have a look at the new list object properties and methods now available in Acrobat 8.0. You should be able to use a combination of those new properties/methods along with some of the original script in this sample in order to come-up with an update solution that works.

  7. Javier on August 8th, 2008

    Perfect!!!!!
    have you any code to get work button “Save” and then save the xml file submit to disk?
    Javier

  8. Javier on August 12th, 2008

    I get to work the Save Button to disk usgin this line : xfa.host.exportData(“”);
    This open a Save As Dialog and it allows to save as xpd format, then you can chage the extension to xml.
    Javier.

  9. Xancholy on December 30th, 2008

    Hi Stefan,

    I am trying to get this to simply sort items in a Dropdownlist in the form:ready event.

    Unlike your example there are no SortAscending or SortByValue checkboxes.

    How would I amend your code to get the items in a dropdownlist to sort ascending when the form loads ?

    How would this line calling the scriptobject change ?
    ScriptObject.Sort(DropdownList, (SortAscending.rawValue == “1”), (SortByValue.rawValue == “1”));

  10. Stefan Cameron on January 5th, 2009

    Xancholy,

    Wouldn’t you simply call

    ScriptObject.Sort(DropdownList, true, false);

    (in other words, “always sort ascending and not by value”) and keep the Sort function the same in your form?

    Also note that doing the sort is much easier now that the new list object API was introduced with Designer/Acrobat 8.0.

  11. Paul Thibideau on May 20th, 2009

    Hi Stefan,

    I am trying to sort a table that contains data entered by the user. Is there a way to sort a table?

    Thanks,
    Paul

  12. Stefan Cameron on May 25th, 2009

    Paul Thibideau,

    If your table rows are instances of a single repeatable row, then you can use the Instance Manager‘s moveInstance() method to re-order rows in a particular sort order of your choice.

    John Brinkman recently posted on sorting subforms. You’ll find some handy sorting scripts in his “Sales Report” sample form.