Search code examples
javasortingjsf-2richfaces

How to sort rows in datatable in RichFaces?


I tried this

<rich:dataTable value="#{myBean.cities}" var="c">

<rich:column>
<f:facet name="header">
 <h:outputText value="City"></h:outputText>
</f:facet>
<h:outputText value="#{c.name}"/>
</rich:column>

<rich:column sortBy="#{c.population}">
<f:facet name="header">
 <h:outputText value="Population"></h:outputText>
</f:facet>
<h:outputText value="#{c.population}"/>
</rich:column>

</rich:dataTable>

but rows aren't sorted.

cities is List<City>, City.population is int


Solution

  • I investigated that in RichFaces 3.3.3 it was enough to add <h:form> .. </h:form> around rich:dataTable but in RichFaces 4 I had to also add:

    • sortOrder attribute to rich:column
    • a4j:commandLink element (within f:facet header) with action pointed at sorting bean to switch asc/desc
    • view scoped managed bean SortBean with org.richfaces.component.SortOrder properties