<a:form> <a:dataTable var="car" value="#{dtFilterView.cars}" widgetVar="carsTable" emptyMessage="No cars found with given criteria" filteredValue="#{dtFilterView.filteredCars}"> <f:facet name="header"> <a:outputPanel> <h:outputText value="Search all fields:" /> <a:inputText id="globalFilter" onkeyup="PF('carsTable').filter()" style="width:150px" placeholder="Enter keyword"/> </a:outputPanel> </f:facet> <a:column filterBy="#{car.id}" headerText="Id" footerText="contains" filterMatchMode="contains"> <h:outputText value="#{car.id}" /> </a:column> <a:column filterBy="#{car.year}" headerText="Year" footerText="lte" filterMatchMode="lte"> <f:facet name="filter"> <a:spinner onchange="PF('carsTable').filter()" styleClass="year-spinner"> <f:converter converterId="javax.faces.Integer" /> </a:spinner> </f:facet> <h:outputText value="#{car.year}" /> </a:column> <a:column filterBy="#{car.brand}" headerText="Brand" footerText="exact" filterMatchMode="exact"> <f:facet name="filter"> <a:selectOneMenu onchange="PF('carsTable').filter()" > <f:selectItem itemLabel="Select One" itemValue="#{null}" noSelectionOption="true" /> <f:selectItems value="#{dtFilterView.brands}" /> </a:selectOneMenu> </f:facet> <h:outputText value="#{car.brand}" /> </a:column> <a:column filterBy="#{car.color}" headerText="Color" footerText="in" filterMatchMode="in"> <f:facet name="filter"> <a:selectCheckboxMenu label="Colors" onchange="PF('carsTable').filter()" panelStyle="width:125px" scrollHeight="150"> <f:selectItems value="#{dtFilterView.colors}" /> </a:selectCheckboxMenu> </f:facet> <h:outputText value="#{car.color}" /> </a:column> <a:column filterBy="#{car.sold}" headerText="Status" footerText="equals" filterMatchMode="equals"> <f:facet name="filter"> <a:selectOneButton onchange="PF('carsTable').filter()"> <f:converter converterId="javax.faces.Boolean" /> <f:selectItem itemLabel="All" itemValue="" /> <f:selectItem itemLabel="Sold" itemValue="true" /> <f:selectItem itemLabel="Sale" itemValue="false" /> </a:selectOneButton> </f:facet> <h:outputText value="#{car.sold ? 'Sold': 'Sale'}" /> </a:column> <a:column filterBy="#{car.price}" headerText="Price" footerText="custom (min)" filterFunction="#{dtFilterView.filterByPrice}"> <h:outputText value="#{car.price}"> <f:convertNumber currencySymbol="$" type="currency"/> </h:outputText> </a:column> </a:dataTable> </a:form>
package com.jarch.showcase.view.data.datatable; import java.io.Serializable; import java.util.List; import java.util.Locale; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.ViewScoped; import org.primefaces.showcase.domain.Car; import org.primefaces.showcase.service.CarService; @ManagedBean(name="dtFilterView") @ViewScoped public class FilterView implements Serializable { private List<Car> cars; private List<Car> filteredCars; @ManagedProperty("#{carService}") private CarService service; @PostConstruct public void init() { cars = service.createCars(10); } public boolean filterByPrice(Object value, Object filter, Locale locale) { String filterText = (filter == null) ? null : filter.toString().trim(); if(filterText == null||filterText.equals("")) { return true; } if(value == null) { return false; } return ((Comparable) value).compareTo(Integer.valueOf(filterText)) > 0; } public List<String> getBrands() { return service.getBrands(); } public List<String> getColors() { return service.getColors(); } public List<Car> getCars() { return cars; } public List<Car> getFilteredCars() { return filteredCars; } public void setFilteredCars(List<Car> filteredCars) { this.filteredCars = filteredCars; } public void setService(CarService service) { this.service = service; } }
package com.jarch.showcase.service; import java.util.ArrayList; import java.util.List; import java.util.UUID; import javax.faces.bean.ApplicationScoped; import javax.faces.bean.ManagedBean; import org.primefaces.showcase.domain.Car; @ManagedBean(name = "carService") @ApplicationScoped public class CarService { private final static String[] colors; private final static String[] brands; static { colors = new String[10]; colors[0] = "Black"; colors[1] = "White"; colors[2] = "Green"; colors[3] = "Red"; colors[4] = "Blue"; colors[5] = "Orange"; colors[6] = "Silver"; colors[7] = "Yellow"; colors[8] = "Brown"; colors[9] = "Maroon"; brands = new String[10]; brands[0] = "BMW"; brands[1] = "Mercedes"; brands[2] = "Volvo"; brands[3] = "Audi"; brands[4] = "Renault"; brands[5] = "Fiat"; brands[6] = "Volkswagen"; brands[7] = "Honda"; brands[8] = "Jaguar"; brands[9] = "Ford"; } public List<Car> createCars(int size) { List<Car> list = new ArrayList<Car>(); for(int i = 0 ; i < size ; i++) { list.add(new Car(getRandomId(), getRandomBrand(), getRandomYear(), getRandomColor(), getRandomPrice(), getRandomSoldState())); } return list; } private String getRandomId() { return UUID.randomUUID().toString().substring(0, 8); } private int getRandomYear() { return (int) (Math.random() * 50 + 1960); } private String getRandomColor() { return colors[(int) (Math.random() * 10)]; } private String getRandomBrand() { return brands[(int) (Math.random() * 10)]; } public int getRandomPrice() { return (int) (Math.random() * 100000); } public boolean getRandomSoldState() { return (Math.random() > 0.5) ? true: false; } public List<String> getColors() { return Arrays.asList(colors); } public List<String> getBrands() { return Arrays.asList(brands); } }