12.1. Sorting

Sorting is an important, even essential, issue for many database applications. For example, look at the application shown in Figure 19 - it is an enhanced version of the service-JSP taken from the sample application (demonstrated in an earlier chapter)

Figure 12.1. DbForms built in sorting mechanism in action

DbForms built in sorting mechanism in action

The user is able to define the sorting of the table by choosing a value Ascending, Descending or None from the sorting-select-boxes below ID or below NAME

If the user wants, for instance, the table to be ordered by NAME instead of ID then he/she has to select ASCENDING or DESCENDING from the NAME drop-down select box.

12.1.1. How to use DbForms sorting facility

The custom tag enabling this functionality is very easy to use:

Example 12.1. Instantiating a sort - tag

          <db:sort fieldName="id" />

Basically, this is all you need to write! This functionality even works within nested forms. But there is one important configuration issue you should be aware of: all non-key-fields you want to be sortable should be declared sortable in the XML-config file:

Example 12.2. Preparing tables for sorting: define sortable fields

          <table name="service">
                <field name="id" fieldType="int" isKey="true"/> 
          <field name="name" fieldType="char" 
    <field name="description" fieldType="char" /> 

The default sorting behavior is false. You should use sortable=true sparingly, especially fields with big field size should not be declared as sortable if it's not really necessary. The reason for this is the amount of data to be transferred for enabling sorting. The more sortable fields, the more data traffic!

Please note: sorting is one of those issues which are still under development. The sorting-boxes described in this tag are already very powerful, but they are still only a beginning. A limitation of the current implementation is the melting of view and controller for the search widgets. Future versions of DbForms may be expected to provide more on sorting.

12.1.2. Alternative method to order query results

An alternative to sorting is to use a orderByattribute in the dbform tag of your jsp page.

        <db:dbform table="TABLE1" followUp="this.jsp" orderBy="id"/>

Similar to sorting, if you use a non-primary key with orderBy, you MUST specify sortable="true" within your dbforms-config.xml file.

If you use orderBy with a non primary key and you don't specify that in the config file, then navNext/navPrev buttons will function like navLast/navFirst buttons and you will skip over results.

See the TagLib for more information.