How to sort a p: dataTable

2

How do I sort a datatable by a text. For example my system is a system of order of services, I need the orders "Open" to come first. Follow the datatable code below. I tried sortby but it did not work.

This is my listAll:

public List<Ordens> listarTodos() { 
String sql = "select o from Ordens as o inner join o.usuario as u where u.id " 
           + " = "+UserStatic.getUsuario().getId(); 

I need to sort by this column:

<p:column filterBy="#{b.situacao}" headerText="Situação Técnico" sortBy="#{b.situacao}" style="width: 98px;"> <h:outputText value="#{b.situacao}" style=" color: red"/> </p:column> 

The 'Open' situation has to come first.

Maybe ordering in sql would work, but I do not know how to do it in sql, if anyone knows. I tried this way more did not work

    public List<Ordens> listarTodos() {
    String sql = "select o from Ordens as o inner join o.usuario as u where      u.id"
            + " = "+UserStatic.getUsuario().getId()
            +"order by o.situacao DESC";
    
asked by anonymous 14.04.2016 / 13:12

2 answers

1

If you want to show your already ordered table you have to sort your list of table objects before showing it.

Or

add sortBy to the table column:

<p:column headerText="Status" sortBy="#{ordem.status}">
      <h:outputText value="#{ordem.status}" />
</p:column>
    
14.04.2016 / 14:15
1

Complementing @Rafael's response, you can also use the sortBy tag inside the dataTable so that the data appears ordered to the user without the need to click on the column to sort.

    <p:dataTable sortBy="{ordem.status}"
                 var="ordem">
            <p:column headerText="Status" sortBy="#{ordem.status}">
                  <h:outputText value="#{ordem.status}" />
            </p:column>
    </p:dataTable>
    
26.04.2016 / 00:01