I'm new to PrimeFaces and to JSF.
I need to filter a table with "contains" match mode. It works fine when the data is static, but when data changes the view doesn't update, and it still shows the old data. It updates when the user changes the filter. If no filter is applied it works fine.
Have you got any suggestions?
I post the code, inspired from the PrimeFaces 6 showcase. It is a page that displays a two columns table and a button that loads a new data for the table
Thank you, any help will be appreciated
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui">
<h:head >
Primefaces Data Table Test
</h:head>
<h:body>
<h:form>
<p:dataTable var="car" value="#{dtCar.cars}" widgetVar="carsTable"
emptyMessage="No cars found with given criteria"
filteredValue="#{dtCar.filteredCars}">
<p:column filterBy="#{car.model}" headerText="Model"
footerText="contains" filterMatchMode="contains">
<h:outputText value="#{car.model}" />
</p:column>
<p:column filterBy="#{car.color}" headerText="Color"
footerText="contains" filterMatchMode="contains">
<h:outputText value="#{car.color}" />
</p:column>
</p:dataTable>
<h:commandButton id="myButton" value="Load" action="#{dtCar.loadNewCars()}"/>
</h:form>
</h:body>
</html>
Here is the CarBean.java
package it.caditech.testtable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name = "dtCar")
@ViewScoped
public class CarBean {
private List<Car> cars;
private List<Car> filteredCars;
public CarBean() {
setCars(new ArrayList<Car>());
getCars().add(new Car("myModel", "blu"));
getCars().add(new Car("punto", "blu"));
getCars().add(new Car("doblo", "grigio"));
getCars().add(new Car("smart", "blu"));
getCars().add(new Car("twingo", "rosso"));
getCars().add(new Car("twingo", "bianco"));
getCars().add(new Car("doblo", "blu"));
// add more cars
}
// getter setter
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public List<Car> getFilteredCars() {
return filteredCars;
}
public void setFilteredCars(List<Car> filteredCars) {
this.filteredCars = filteredCars;
}
public String loadNewCars() {
if (getCars().get(0).getModel().equals("myModel")) {
getCars().clear();
getCars().add(new Car("gt", "giallo"));
getCars().add(new Car("scirocco", "azzurro"));
getCars().add(new Car("duna", "giallo"));
getCars().add(new Car("diablo", "griggio"));
getCars().add(new Car("cayenne", "rosso"));
getCars().add(new Car("aygo", "azzurro"));
setFilteredCars(null);
}
else {
getCars().clear();
getCars().add(new Car("myModel", "blu"));
getCars().add(new Car("punto", "blu"));
getCars().add(new Car("doblo", "grigio"));
getCars().add(new Car("smart", "blu"));
getCars().add(new Car("twingo", "rosso"));
getCars().add(new Car("twingo", "bianco"));
getCars().add(new Car("doblo", "blu"));
setFilteredCars(null);
}
return null;
}
}
And here is the Car.java
package it.caditech.testtable;
public class Car {
private String model;
private String color;
public Car(String m, String c) {
model = m;
color = c;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}