views:

7331

answers:

10

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)
+3  A: 

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
A: 

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

longeasy
I cannot get this attribute to work consistently. Anyway he said server side sorting. Isn't rich:columns with sortBy client side sorting?
A: 

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

Shervin
A: 

The solution provided by Sblundy is a good one. But there has to be a proper way of doing it. Server-side sorting and grouping(in case of ExtendedDatatable) is a must have feature for libraries like richfaces. I am unable to find any help so far and I really cant believe that I am the only one trying to do server side stuff.

Pauli
A: 

The functionality of sortBy is limited to client-side although it does hit the server to get the list sorted. Is there any way of overridding the server-side sort function?

Pauli
A: 

I am also looking for rich:datatable approach with server side sorting. Client side sorting is practically incorrect. The end users will get mad when they see that only the current view results are getting sorted and not the ENTIRE DATA.

bart007
sorting functionality provided by the Rich faces is like a replica of server side sorting, because when the data table is loaded the entire data will there in the list that is tagged to the data table. The only drawback of technique is if your data table has a huge number of data, then it takes time to load at the starting.
Hari
that's why, server side sorting is preferred
Hari
A: 

Your datamodel needs to implement "Modifiable" interface.

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

Philipp
A: 

"Your datamodel needs to implement "Modifiable" interface.

The datatable will call it's modify() method to do serverside sorting and filtering." how could this be done??

ikren
A: 

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
A: 

I had the same problem. Client-side sorting/filtering was not acceptable. Moreover, when I clicked on a column to start sorting by that column my DataScroller stopped working. In my case I was using extendedDataTable, ExtendedDataModel.

User ikren is right. Everything started working, row paging using datascroller too. Client stopped sorting and filtering by itself. It started using model's modify method, so I could pass filters to my data provider. ikren thank you very much!

zephid