2

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;
    }

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Giovanni Cornachini
  • 151
  • 1
  • 3
  • 11
  • Not related : JSF Calls getters multiple times, so you it should not contain any database access. it should look excatly like this : public X getY() { return y; } http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times/2090062#2090062 – S19 Sep 12 '14 at 08:47

2 Answers2

14

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;
Danny Taberham
  • 156
  • 1
  • 2
1

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.

wittakarn
  • 3,124
  • 1
  • 18
  • 31
  • 1
    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. – Yannick Sep 12 '14 at 03:35