简体   繁体   English

Primefaces“sortBy =”不起作用

[英]Primefaces “sortBy=” doesn't work

I'm developing an application using Primefaces + JSF. 我正在使用Primefaces + JSF开发一个应用程序。 My data table works, but has a problem at sort sortBy= , I tried sortBy="#{pc.rota}" but it doesn't work too: 我的数据表有效,但排序sortBy=有问题,我试过sortBy="#{pc.rota}"但它也不起作用:

Data table show all rows, the problem I think is sortBy= or my backing bean. 数据表显示所有行,我认为问题是sortBy=或我的支持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;
    }

}

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. 要使排序工作,您需要每次使用getter返回相同的列表对象,在这种情况下,您每次都会从dao返回一个新列表。 So you should only fetch a new list if the list is previously null. 因此,如果列表以前为空,则只应获取新列表。 The code inside your getter should be as below. getter中的代码应如下所示。

   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;

As far as I know, sortBy attribute of Datatable is applied for only Primitive Data Types and String. 据我所知,Datatable的sortBy属性仅适用于原始数据类型和字符串。 If rota is an object, you must create method for sorting by yourself. 如果rota是一个对象,则必须自己创建排序方法。 Alternative, using sortBy="#{pc.rota.someting}" that contain Primitive Data Types or String for sorting. 或者,使用包含原始数据类型或字符串的sortBy =“#{pc.rota.someting}”进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM