4
votes

I have a PrimeFaces 3.2 DataTable using apache myfaces 2.0.2 . I want a initial sort. My JSF looks like:

<p:dataTable  id="serverdata" var="serverdata" sortBy="#{serverdata[0]}" sortOrder="descending" 
                value="#{ serverDataTable.list }" rows="10" editable="true" 
                paginator="true" rowsPerPageTemplate="10,20,50"
                paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}">
                <f:facet name="header">  
                   Datatable
                 </f:facet>
                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Datum"/>
                    </f:facet>
                    <h:outputText value="#{serverdata[0]}">
                    </h:outputText>
                </p:column>

But I get a UnsupportedOperationException: The result list is read-only. When I delete the sortBy tag in it works fine.

So my question: How can I realize an initial sort?

1

1 Answers

4
votes

The sorting is executed on the original List. If it is read-only, an Exception is thrown. Try the same, but with a regular, mutable List

EDIT : create a regular list in the bean:

public class MyBean {
...
List<MyObject> myList = new ArryList<MyObject>();
myList.add(new MyObject("a"));
myList.add(new MyObject("b"));
myList.add(new MyObject("c"));
...

then use sorting like this : value="#{myBean.myList}" var="myVar" sortBy="#{myVar.stringProp}"

EDIT2: If the sorting parameters are strings or numbers, you do not need to customize anything. If you are sorting anything else, you will have to define a custom sortFunction,

If you use advanced options like lazy loading (which is btw broken in pf 3.2 - you can update to 3.3 now - it has been released on 29.5 and claims to have solved this), you will have to define a custom model, taking care of filtering and sorting yourself.

So if you want to sort your data by the contents of an array, you would need to define the attribute sortFunction="#{myBean.sortData()}"