8

I have a data table with a variable number of columns and a data scroller. How can I enable server side sorting? I prefer that it be fired by the user clicking the column header.

<rich:datascroller for="instanceList" actionListener="#{pageDataModel.pageChange}"/>
<rich:dataTable id="instanceList" rows="10" value="#{pageDataModel}"
                var="fieldValues" rowKeyVar="rowKey">
  <rich:columns value="#{pageDataModel.columnNames}" var="column" index="idx">
    <f:facet name="header">
      <h:outputText value="#{column}"/>
    </f:facet>          
    <h:outputText value="#{classFieldValues[idx]}" />
  </rich:columns>
</rich:dataTable>

I already have a method on the bean for executing the sort.

public void sort(int column)
sblundy
  • 60,628
  • 22
  • 121
  • 123

5 Answers5

6

I ended up doing it manually. I adding a support tag to the header text tag, like so.

<h:outputText value="#{column}">
  <a4j:support event="onclick" action="#{pageDataModel.sort(idx)}"
               eventsQueue="instancesQueue"
               reRender="instanceList,instanceListScroller"/>
</h:outputText>

To get the ascending/descending arrows, I added a css class.

<h:outputText value="#{column}" styleClass="#{pageDataModel.getOrderClass(idx)}" >
  <a4j:support event="onclick" action="#{pageDataModel.sort(idx)}"
               eventsQueue="instancesQueue"
               reRender="instanceList,instanceListScroller"/>
</h:outputText>
sblundy
  • 60,628
  • 22
  • 121
  • 123
1

Your datamodel needs to implement "Modifiable" interface.

The datatable will call it's modify() method to do serverside sorting and filtering.

EdChum
  • 376,765
  • 198
  • 813
  • 562
Philipp
  • 11
  • 1
1

There is a fairly elegant solution to this solution here:

http://livedemo.exadel.com/richfaces-demo/richfaces/sortingFeature.jsf?tab=ex-usage

This demo avoids using the tag.

Marco
  • 8,958
  • 1
  • 36
  • 56
  • 2
    This shows how to do external sorting, but the sorting is still happening on the client side. I believe that OP is interested in catching the click event from clicking the headers and then having the underlying datasource sorted on the backend. – Jason Wheeler Feb 07 '12 at 18:47
0

Have a look at the "sortBy" property of "rich:columns", maybe that's what you're looking for. Richfaces Reference

Patrick
  • 1,328
  • 10
  • 19
  • 2
    I cannot get this attribute to work consistently. Anyway he said server side sorting. Isn't rich:columns with sortBy client side sorting? –  Aug 18 '09 at 17:07
0

Cant you just use Collection.sort() when you retrieve the List?

Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143