0

I have the problem in a JSF / xhtml page, that my primefaces datatable won't sort and filter correctly. 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:

<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:

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!

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
curr11
  • 11
  • 2
  • 1
    PrimeFaces showcse works perfectly fine... – Kukeltje Nov 27 '18 at 10:35
  • Off topic: what are all those properties (which look like contract properties)? It will probably be better to have a `private Contract selectedContract` property for those. – Jasper de Vries Nov 27 '18 at 10:45
  • @JasperdeVries I use this class "proposalController" also for saving a new Contract with respective properties. Would you suggest making a separate class for that? – curr11 Nov 27 '18 at 10:53
  • 1
    see https://stackoverflow.com/questions/10301363/jpa-entity-as-jsf-bean for the 'gather/scatter' (anti) pattern you use to which @JasperdeVries refers. – Kukeltje Nov 27 '18 at 13:20
  • When calling sort by clicking on the sort link in the column header the method is called to populate the data table which can result in nothing really happening. I had that same problem before and solved it by setting the list in the PostConstruct of the backing bean and a separate method to return the list that was populated in the PostConstruct. I can't say about the filtering input boxes as I don't use them but it could be related to the same problem. – ChadNC Nov 27 '18 at 20:19
  • @ChadNC thanks for your answer. Just did that, but unfortunately the problem still occurs. – curr11 Nov 28 '18 at 10:54
  • I asked a coworker who is working on different project and is using a filtered datatable and he mentioned the following items. 1) you don't have a filteredValue set on the datatable. 2) your columns do not have the facet "filter" defined. I took a look at the primefaces showcase and both are there in the example. http://primefaces.org/showcase/ui/data/datatable/filter.xhtml – ChadNC Nov 30 '18 at 16:21

1 Answers1

0

In your getContractList() method, you return a list from some service, which is probably a new list everytime the method is called. 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.

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;
    }
}
Martin Höller
  • 2,714
  • 26
  • 44
  • Thanks for the answer. I just tried that but unfortunately it didn't change anything for my problem..Is this an indication that the problem maybe is on the jsf page side? – curr11 Nov 27 '18 at 12:51
  • I don't see any other obvious problem in your code. Do you get any error-logs on your server or the JavaScript console of your browser? What versions of PrimeFaces and JSF are you using? – Martin Höller Nov 27 '18 at 13:30
  • Glassfish Server log and JavaScript console log both are completely empty – curr11 Nov 27 '18 at 13:57