Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With the native <code>&lt;h:dataTable&gt;</code> you have to do the sorting yourself in your managed bean. You could use existing JSF extension libraries that have them built in such as:</p> <ul> <li>MyFaces Tomahawk - <a href="http://myfaces.apache.org/tomahawk-project/tomahawk/tlddoc/t/dataTable.html" rel="nofollow">docs</a></li> <li>ICEfaces - <a href="http://facestutorials.icefaces.org/tutorial/dataTable-tutorial.html" rel="nofollow">docs</a></li> <li>RichFaces - <a href="http://docs.jboss.org/richfaces/latest_4_3_X/Component_Reference/en-US/html_single/#sect-Component_Reference-Tables_and_grids-Table_sorting" rel="nofollow">docs</a></li> <li>PrimeFaces - <a href="http://www.primefaces.org/showcase/ui/datatableLazy.jsf" rel="nofollow">docs</a></li> <li>etc, way too many to state.</li> </ul> <p>But if you don't want to use the above toolkits, then in your managed bean, you define your List and sort order (asc or dec). It can be as simple or complex you want.</p> <p>Change the <code>Ordering</code> library to the <code>SortOrder</code> library, referencing this library: <code>import org.richfaces.component.SortOrder;</code></p> <p>The sort order comparator can be defined in a variable programmatically using the <code>&lt;rich:column&gt;</code> attribute:</p> <pre><code>private SortOrder sorting = SortOrder.unsorted; </code></pre> <p><a href="http://javaevangelist.blogspot.com/2013/01/jsf-2x-tip-of-day-richfaces-table.html" rel="nofollow">This is an example of using SortOrder programmatically using JSF 2.x/RichFaces 4.x</a>. It uses a three-state sort method: unsorted (default), ascending, and descending, and implemented by setting the sortOrder attribute.</p> <p>Or the comparator default behavior can be overridden in code, as in this example:</p> <pre><code>@ManagedBean(name="somebean") @SessionScoped public class OrderBean implements Serializable { private static final long serialVersionUID = ....; private List&lt;Item&gt; items; private boolean sortAscending; ... } </code></pre> <p>In your view, you define the which headers you want to sort with, so add a commandLink to make each header clickable.</p> <pre><code>&lt;h:dataTable value="#{somebean.items}" var="i"&gt; &lt;h:column&gt; &lt;f:facet name="header"&gt; &lt;h:commandLink action="#{somebean.sort}"&gt; Sort Column&lt;/h:commandLink&gt; &lt;/f:facet&gt; #{i.name} &lt;/h:column&gt; &lt;/h:dataTable&gt; </code></pre> <p>Now you have to implement the sort for your bean with basic collections, again, it can be as complex as you can:</p> <pre><code>private final Comparator NAME_SORT_ASC = new Comparator&lt;Item&gt;() { @Override public int compare(Item o1, Item o2) { return o1.getName().compareTo(o2.getName()); } } }; private final Comparator NAME_SORT_DESC = new Comparator&lt;Item&gt;() { @Override public int compare(Item o1, Item o2) { return o2.getName().compareTo(o1.getName()); } } }; public String sort() { Collections.sort(items, sortAscending ? NAME_SORT_ASC : NAME_SORT_DESC ); } </code></pre> <p>You can make your life easier by reusing stuff instead of doing that for each column, I will let you figure that out. You can use better libraries for Java to help you do the comparator for example with <a href="http://code.google.com/p/guava-libraries/wiki/OrderingExplained" rel="nofollow">Guava</a> from Google or <a href="http://commons.apache.org/collections/" rel="nofollow">Collection Commons</a> from Apache.</p> <p>Instead of doing all that, and reinventing the wheel, use a framework that abstracted all this out for you, they make your life way easier..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload