How to sort a p:datatable

Asked

Viewed 1,400 times

2

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

This is my list:

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 through 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 situation 'In Open' has to come first.

maybe sort sql would work, but I don’t know how to do sql if anyone knows. I tried so but it didn’t 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";
  • Welcome to Stack Overflow! You used the comments area to add clarifications or ask a question. Instead, it is better to [Dit] your question and include that content right there. For that, there is a [Dit] link below the question. Thus, the content is all gathered in one place, and those who arrive here need not be looking for information in various comments to understand the problem.

  • I posted the code right above.

2 answers

2


Complementing the @Rafael response, you can also use the sortBy tag inside the datatable for the data to appear ordered to the user without the need to click in 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>

1

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

Or

add the sortBy in the table column:

<p:column headerText="Status" sortBy="#{ordem.status}">
      <h:outputText value="#{ordem.status}" />
</p:column>
  • The problem would be that the sortBy have to click to order.. This screen is on a monitor, would have to order automatic. How do I sort the list of objects ? could help me ?

  • Add an order by to your sql query, to bring sorted by Open

  • I don’t know much about sql. would look like this ? "select from Orders as Inner Join o.usuario as u Where u.id order by situacao = 'Open' "

  • order by o.status DESC or order by o.status ASC, for descending or ascending. You have to send the column not value, follow example of order by , http://www.w3schools.com/sql/sql_orderby.asp

  • I put my code in the description as it is now. and it didn’t work out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.