Primefaces "sortBy=" doesn't work

14,530

Solution 1

For the sort to work you need to return the same list object each time with the getter, where in your case you are returning a new list from the dao every time. So you should only fetch a new list if the list is previously null. The code inside your getter should be as below.

   if (allPCEmulation == null) {
       PCEmulationDAO dao = new PCEmulationDAO();
        try {
            allPCEmulation = dao.getAll();

        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Problema no metodo list : " + e);
        }
    }

    return allPCEmulation;

Solution 2

As far as I know, sortBy attribute of Datatable is applied for only Primitive Data Types and String. If rota is an object, you must create method for sorting by yourself. Alternative, using sortBy="#{pc.rota.someting}" that contain Primitive Data Types or String for sorting.

Share:
14,530
Giovanni Cornachini
Author by

Giovanni Cornachini

Web developer

Updated on July 24, 2022

Comments

  • Giovanni Cornachini
    Giovanni Cornachini almost 2 years

    I'm developing an application using Primefaces + JSF. My data table works, but has a problem at sort sortBy=, I tried sortBy="#{pc.rota}" but it doesn't work too:

    Data table show all rows, the problem I think is sortBy= or my backing bean.

    page.xhtml

    <h:body>
    
        <h:form id="pcEmulation">
    
            <p:dataTable id="dataTablePCEMulation"  var="pc" value="#{pCEmulationBean.allPCEmulation}"   
    
                         rows="10"                         
                         rowsPerPageTemplate="5,30,50,100,200,300"
    
    
                         emptyMessage="Não foi encontrado"
                         >
    
    
                <f:facet name="header">
                    PC Emulation Web
                </f:facet>
    
                <p:column headerText="PC - TX OLO's"  filterValue="#{pc.filtpcn}" filterMatchMode="contains" filterBy="#{pc.filtpcn}" >                
                    <h:outputText value="#{pc.filtpcn}" />
                </p:column>
    
                <p:column headerText="Rota" sortBy="rota" >                
                    <h:outputText value="#{pc.rota}" />
                </p:column>
    
                <p:column headerText="Origem">                    
                    <h:outputText value="#{pc.origem}" />
                </p:column>
    
                <p:column headerText="Antigo">
                    <h:outputText value="#{pc.epcn}" />
                </p:column>
    
                <p:column headerText="Destino">
                    <h:outputText value="#{pc.destino}" />
                </p:column>
    
                <p:column headerText="PC-Novo">
                    <h:outputText value="#{pc.realpcn}" />
                </p:column>
    
    
            </p:dataTable>
    
            <p:blockUI block="dataTablePCEMulation" trigger="dataTablePCEMulation">
                LOADING<br />
                <p:graphicImage value="/images/loading.gif"/><br />
                <p:graphicImage value="/images/tim-banner2.png" width="100px" height="45px"/>
            </p:blockUI>
    
        </h:form>
    </h:body>
    

    Backing bean:

    @ManagedBean
    //@ViewScoped
    @SessionScoped
    public class PCEmulationBean {
    
        public List<PCEmulation> allPCEmulation;
    
    
    
        public List<PCEmulation> getAllPCEmulation() {
            PCEmulationDAO dao = new PCEmulationDAO();
            try {
                allPCEmulation = dao.getAll();
    
            } catch (ClassNotFoundException | SQLException e) {
                System.out.println("Problema no metodo list : " + e);
            }
    
            return allPCEmulation;
        }
    
    }
    
  • Yannick
    Yannick over 9 years
    To use sortBy with Objets you need to specify the sortFunction attribute pointing on a method that returns the same set of values as the compareTo method from the Comparable interface.