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 sqltemplateconstraints 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:
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 orderboth,
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.