SourceForge.net Logo
Home | SourceForge | Documentation | FAQ | What's New | To-Do List | Forums | Download | Contact

Sparse - Sorting

In addition to searching and navigating normally, Sparse also allows the user to sort your data however he likes. Obviously you can "hard-code" the sorting by including it in the sqltemplate constraints attribute, but there's a great way to allow the user to control the sorting himself. The easiest way to do this is to use the sqlsort tag. This prints a small form which instantly allows the user to sort his data by whichever field he likes. Let's take a simple example here, copied from the one we looked at when searching:

<sqltemplate database="myDB" tables="news" type="display" limit="5">
  <table border>
    <sqlsort names="name, title" labels="Name", "News Title" order="asc">
    <sqlrow>
      <tr><th>Name</th><th>News Title</th><th>E-mail</th></tr>
      <tr><td><sqlfield name="name"></td>
           <td><sqlfield name="title"></td>
           <td><sqlfield name="email"></td>
      </tr>
   </sqlrow>
  </table>
</sqltemplate>

That'll print out something like the following:

Sort By:
Name News Title E-mail
Daniel First Update! my_email@email.com
Sarah Second Update! her_email@mail.com
Magic Johnson Update! magic@the-gathering.com
Terry Discworld Update! pratchett@rocks.com
Boutros I Can't Pronounce My Name hgfetbcvnb@serbia.com

The user can select whichever field he wants to sort, click "Sort", and off we go! Note the use of the names attribute. This limits our sorting to two fields: name and title. If we left names out entirely, all three fields would be included in the select box by default. Also note that the labels attribute is a comma-delineated list of labels exactly corresponding to the names. The names are the internal name of the field, required for the actual sorting, whereas the labels are what's actually displayed on the select box.

You can enclose labels in quotations; this allows you to use commas in the labels.

Also note that we gave it an order: asc, or ascending. If we left out the order, or used the order both, a second field would be printed:

Sort By:

Now the user can decide whether it's ascending or descending!

Sorting By Multiple Fields

sqlsort also allows the user to sort by more than one field at once. All we have to do is include one more attribute, levels, and set it to 2. Now the form that's printed will look like this:

Sort By: ,

We can also leave out the order attribute and it'll just be this:

Sort By:

Unfortunately there's no way to indicate that the first one has to always be ascending and the second descending, or to associate different fields with automatic ascending/descending orders.

You can use Linking to sort via a link, e.g. in table headers.

Previous: Enumsets | Searching | Next: Working With Data