Sort Subforms

You probably already knew it was possible to re-order (and hence sort) a group of subforms.  But in case you didn’t — or in case you needed a sample, here you go.

In the sample, the column headings are button fields.  Their click event sorts the rows by the values in that column.  If you click again it will sort in descending order.

I centralized the sort methods into a script object that ought to be re-usable in other contexts:

/**
* Sort a group of subforms.  Calling this the first time will sort
* ascending. Calling it again will toggle to descending.
* @param vButton the button field used to initiate the sort. 
*  We store the current sort order in the button rawValue property.
* @param vInstanceManager the instance manager of the subforms to
*  be sorted
* @param vSortFieldName the name of the field in the subforms used
*  to sort.  Null values always sort to the beginning.
*/
function sort(vButtonField, vInstanceManager, vSortFieldName)

/**
* Sort a group of subforms.  Calling this the first time will sort
* ascending. Calling it again will toggle to descending.
* @param vSubformInstance one instance of a subform in the set of
*  subforms to be sorted
* @param vFieldName the name of the field used to sort
* @param bAscending boolean: if true, sort in ascending order,
*  otherwise descending.
*/
function performSort(vSubformInstance, vFieldName, bAscending)

Note that sorting subforms will also cause their underlying data to also be re-ordered.

Notes

Rather than embarrass myself by implementing a rookie bubble sort, I used the JavaScript Array.sort() method.  Of course, this meant figuring out how to set up a sorting function and letting it have access to the sort parameters (sort field and sort order).  But figuring out how to store these under <extras> was easier than trying to impress you with a fancy heapsort algorithm.

I generated the sample data at this very clever website: http://www.generatedata.com/ 

Deep End

If you sort a very large set of subforms, you might run into performance issues.  If you need to improve the performance, try sorting the underlying data DOM using the remove/insert methods on the nodelist.  After re-ordering the data you would do an xfa.form.remerge() and the subforms should show up nicely sorted.

8 Responses to Sort Subforms

  1. Mark Jones says:

    Thanks for the excellent work you have done here John. I am new to this whole word of PDF forms and was trying my best to understand how to achieve what you have put together and so graciously shared with everyone. Quick question, I notice that your form is pulling in data versus the user being able to populate the fields themselves. Is there a way to do this without breaking what you’ve already got in place (ie. the sorting functionality?) or will it only work with data that is reference from another source?

    Thanks,
    Mark

  2. Mark:
    This form is created using Adobe Form Designer. In this example I created the fields “read-only”. It’s also possible to make them interactive to allow user input.

    John

  3. Lawrence says:

    Hi John,
    I really like what you did with this sorting thing. I would like to do the same thing. I have downloaded your form. I open it up in livecycle and changed the name of the buttons. Then when I preview it, the table is gone except for the button row. What am I doing wrong?

    • John Brinkman says:

      Lawrence:
      I suspect you need to supply data for the preview.
      To re-use the same data, take the original downloaded form, open in Acrobat, and export the data.
      (Tools/Forms/More Form Options/Manage Form Data/Export Data… )
      Then specify that data file in the designer preview properties.

      Good luck
      John

  4. Lawrence says:

    I did what you recommended and this worked, However, the only feature I want to utilize off of your sample form is the sort feature, I would like to change the button names, for example instead of birthday, I want it to say Date….and I would like to be able to input in dates in these cells myself. I know how to un do the read only, however I still cannot get the sort to work when I put in my own cells. Any help would be greatly appreciated.

    • Lawrence:
      If you change the names of the fields or subform, you will need to also change the script.
      The actual sort routine is generic, but there are click events on the headers that will need to be changed. You will see a call that looks like:
      SubformSort.sort(this, _SalesPerson, “FirstName”);
      _SalesPerson is the name of the instance manager that controls the rows. If you change the subform name to “Person”, this parameter would be _Person.
      The last parameter is the name of the field to sort by.
      Good luck

      John

  5. Karen says:

    I’ve tried to download the pdf file to see the code and compare with what I’m doing however when I open it in LiveCycle it says it’s not a valid pdf.

    I have a basic form built that’s working fine but now the stakeholders want me to see if I can sort by a column in a table. I’m not a coder so I’m trying to decypher scripts to match my data.

    Any help you can offer would be great! Thanks, Karen