RSS

Forums/New Features

Fieldset ordering beaten into submission

Michael Slater Nov 01

Fieldsets, which are groups of fields that can be repeated any number of times as part of a database item, are very handy when you need a defined structure with a varying number of entries. For example, you might use a field set for "related links", and have one field for name and one field for URL. (See Using Fieldsets for Repeating Sets of Fields for details.)

There was one hitch, however: while fieldset entries generally stayed in the same order as they were entered, it wasn't exactly predictable, and you couldn't choose how to sort the entries.

Now there is a numeric position value automatically added to each set in all fieldsets, and it is filled in sequentially as sets are created. So if you ignore it, you'll get the same behavior as before -- items appear, by default, in the order in which they were entered.

But now you have two new benefits:

  1. The order of the items persists reliably, even if you edit some of them.
  2. You can modify the order in which the items will be shown by editing the Position field in each set.

In addition, you can now choose to sort the items by another field, if you want.

For example, here's some code that loops through all of the Project items, and for each one, shows its name and then lists all of its references. The references are stored in a fieldset, with a ref_title field and a ref_url field. The list of references is sorted by the title (ref_title).

<w:kb:item:each type='project' >
  <h1>Project: <w:name /></h1>
  <h2>References</h2>
  <ul>
    <w:kb:item:for_each in="references" sort="in.ref_title" >
      <li><a href="<w:get name='in.ref_link'>"><w:get name="in.ref_title" /></a></li>
    </w:kb:item:for_each>
  </ul>
</w:kb:item:each>