2

The sortBy attribute of <p:column> in <p:dataTable> lets the user to sort a column ascending or descending on each click, is there anyway to sort column descending on the first click of column header. It is ascending by default.

<p:column sortBy="#{bean.col1Value}" headerText="Col Header">
   #{bean.col1Value}
</p:column>

Is there any possibility to override this <p:dataTable>'s default setting?

Tiny
  • 27,221
  • 105
  • 339
  • 599
rashidnk
  • 4,096
  • 2
  • 22
  • 32

2 Answers2

2

I think you can use the attribute sortFunction. I quote from the Primefaces 5.1 User Guide page 153-154

Instead of using the default sorting algorithm which uses a java comparator, you can plug-in your own sort method as well

public int sortByModel(Object car1, Object car2) {
     car2.compareTo(car1);
}

And then in your html file

<p:dataTable var="car" value="#{carBean.cars}">
<p:column sortBy="#{car.model}" sortFunction="#{carBean.sortByModel}"
headerText="Model">
<h:outputText value="#{car.model}" />
</p:column>
...more columns
</p:dataTable>
rashidnk
  • 4,096
  • 2
  • 22
  • 32
Fritz
  • 1,144
  • 1
  • 13
  • 21
  • 1
    So you effectively 'reverse' the sorting in sorting algorithm... But did you check what the icons on the column do then? Most likely they do not match up then with the sorting. – Kukeltje Sep 04 '15 at 08:30
  • i didnt try this, so what will sortByModel function returning ? – rashidnk Sep 04 '15 at 12:15
  • @rashidnk: it's a java 'comparator': http://stackoverflow.com/questions/2839137/how-to-use-comparator-in-java-to-sort – Kukeltje Sep 04 '15 at 13:09
  • @rashidnk as mentioned in the comment before this it should return your custom sorting algorithm to suit your requirements, i.e, to default to descending sort on your first click. – Fritz Sep 05 '15 at 09:41
-1

You need to use sortFunction="#{testBean.customSort}" and you can customized your sorting.

vinod
  • 1,178
  • 4
  • 16
  • 42