简体   繁体   English

Primefaces Datatable-排序不起作用

[英]Primefaces Datatable - Sorting doesn't work

I have the problem in a JSF / xhtml page, that my primefaces datatable won't sort and filter correctly. 我在JSF / xhtml页面中遇到问题,我的primefaces数据表无法正确排序和过滤。 I populate the datatable from my database, which works fine. 我从数据库中填充数据表,效果很好。 However, as soon as I start to type something into the filter, or click on the header of a column to sort, all the entries disappear. 但是,一旦我开始在过滤器中键入内容,或单击列标题进行排序,所有条目就会消失。 I don't really know where my error here is. 我真的不知道我的错误在哪里。 This is the code of the datatable in JSF: 这是JSF中数据表的代码:

<p:dataTable var="c" value="#{proposalController.contractList}">

    <p:column  headerText="ID" sortBy="#{c.id}" filterBy="#{c.id}" filterMatchMode="contains">
        <h:outputText value="#{c.id}" />
    </p:column>

    <p:column headerText="Customer Name" sortBy="#{c.custName}" filterBy="#{c.custName}" filterMatchMode="contains">
        <h:outputText value="#{c.custName}" />
    </p:column>

    <p:column headerText="Asset" sortBy="#{c.asset}" filterBy="#{c.asset}" filterMatchMode="contains">
        <h:outputText value="#{c.asset}" />
    </p:column>


</p:dataTable>

And this is the java code behind it: 这是其背后的Java代码:

package controller;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import model.Contract;
import model.ContractManager;

@ManagedBean (name="proposalController")
@SessionScoped
public class proposalController {

    @EJB
    private ContractManager contract;
    private int id;
    private String custName;
    private String custStreet;
    private String custZIP;
    private String custCity;
    private int creditScore;
    private String custPers;
    private String mail;
    private String phone;
    private Date start;
    private Date endd;
    private String asset;
    private String insurance;
    private double rate;
    private String status;
    private List<Contract> contractList;  




    public List<Contract> getContractList() {
        this.contractList = contract.getContractList();
        return this.contractList;
    }

It would be great if someone could help me! 如果有人可以帮助我,那将是很好!

In your getContractList() method, you return a list from some service, which is probably a new list everytime the method is called. 在您的getContractList()方法中,您从某些服务返回一个列表,每次调用该方法时,它可能都是一个新列表。 You should instead get this list just once from your service on initialization (some @PostConstruct -annotated method for instance), save it in your session-bean and return the list from your session-bean instead. 相反,您应该在初始化时从服务中一次获取此列表(例如,某些@PostConstruct -annotated方法),将其保存在会话bean中,然后从会话bean返回列表。

Something like this: 像这样:

@ManagedBean (name="proposalController")
@SessionScoped
public class proposalController {
    @EJB
    private ContractManager contract;

    private List<Contract> contractList;

    @PostConstruct
    public void init() {
        contractList = contract.getContractList();
    }

    public List<Contract> getContractList() {
        return this.contractList;
    }
}

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

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