Filtering updates the data based on the constraints.
<style>
.ui-filter-column .ui-column-customfilter .custom-filter {
width: 100%;
box-sizing: border-box;
}
</style>
<div class="card">
<h5>Column Filtering</h5>
<h:form>
<p:dataTable var="customer" value="#{dtFilterView.customers1}" widgetVar="customersTable"
emptyMessage="No customers found with given criteria"
filteredValue="#{dtFilterView.filteredCustomers1}"
globalFilterOnly="#{dtFilterView.globalFilterOnly}">
<f:facet name="header">
<div class="flex align-items-center justify-content-between">
<span>List of Customers</span>
<div>
<p:inputText id="globalFilter" onkeyup="PF('customersTable').filter()"
style="width:10rem"
placeholder="Enter keyword"/>
<p:commandButton value="Global Filter Only" update="@form" styleClass="ml-2"
action="#{dtFilterView.toggleGlobalFilter()}"/>
</div>
</div>
</f:facet>
<p:column headerText="Name" filterBy="#{customer.name}" sortable="false"
filterMatchMode="contains" filterPlaceholder="Filter By Name">
<h:outputText value="#{customer.name}"/>
</p:column>
<p:column headerText="Country" sortable="false"
filterBy="#{customer.country.name}">
<span class="flag flag-#{customer.country.code}" style="width: 30px; height: 20px"/>
<h:outputText style="vertical-align: middle; margin-left: .5rem"
value="#{customer.country.name}"/>
</p:column>
<p:column field="representative" headerText="Representative" filterMatchMode="exact"
sortable="false">
<f:facet name="filter">
<p:selectOneMenu onchange="PF('customersTable').filter()" styleClass="ui-custom-filter">
<f:selectItem itemLabel="All" itemValue="#{null}" noSelectionOption="true"/>
<f:selectItems value="#{dtFilterView.representatives}"/>
</p:selectOneMenu>
</f:facet>
<p:graphicImage name="images/avatar/#{customer.representative.image}" library="demo" width="32"
style="vertical-align: middle"/>
<h:outputText style="vertical-align: middle; margin-left: .5rem"
value="#{customer.representative.name}"/>
</p:column>
<p:column field="date" headerText="Join Date" filterMatchMode="between" sortable="false">
<f:facet name="filter">
<p:datePicker selectionMode="range" onchange="PF('customersTable').filter()" styleClass="white-space-nowrap"
converter="jakarta.faces.DateTime" showOnFocus="false" showIcon="true"/>
</f:facet>
</p:column>
<p:column field="status" headerText="Status" filterMatchMode="exact" sortable="false">
<f:facet name="filter">
<p:selectOneMenu onchange="PF('customersTable').filter()" styleClass="ui-custom-filter">
<f:selectItem itemLabel="All" itemValue="#{null}" noSelectionOption="true"/>
<f:selectItems value="#{dtFilterView.customerStatus}"/>
</p:selectOneMenu>
</f:facet>
<span class="customer-badge status-#{customer.status.statusName}">#{customer.status.statusName}</span>
</p:column>
<p:column field="activity" headerText="Activity" filterMatchMode="gt" sortable="false"
converter="jakarta.faces.Integer">
<p:progressBar value="#{customer.activity}" displayOnly="true" style="height: .5rem"/>
</p:column>
</p:dataTable>
</h:form>
</div>
<div class="card">
<h5>Default Filter</h5>
<h:form>
<p:dataTable var="customer" value="#{dtFilterView.customers2}" widgetVar="customersTable2"
emptyMessage="No customers found with given criteria"
filteredValue="#{dtFilterView.filteredCustomers2}" filterBy="#{dtFilterView.filterBy}">
<f:facet name="header">
<span>List of Customers</span>
</f:facet>
<p:column headerText="Name" sortBy="#{customer.name}" filterBy="#{customer.name}">
<h:outputText value="#{customer.name}"/>
</p:column>
<p:column headerText="Country" sortBy="#{customer.country.name}"
filterBy="#{customer.country.name}">
<span class="flag flag-#{customer.country.code}" style="width: 30px; height: 20px"/>
<h:outputText style="vertical-align: middle; margin-left: .5rem"
value="#{customer.country.name}"/>
</p:column>
<p:column field="representative" headerText="Representative" filterMatchMode="exact"
sortBy="#{customer.representative.name}">
<f:facet name="filter">
<p:selectOneMenu onchange="PF('customersTable2').filter()" styleClass="custom-filter">
<f:selectItem itemLabel="All" itemValue="#{null}" noSelectionOption="true"/>
<f:selectItems value="#{dtFilterView.representatives}"/>
</p:selectOneMenu>
</f:facet>
<p:graphicImage name="images/avatar/#{customer.representative.image}" library="demo" width="32"
style="vertical-align: middle"/>
<h:outputText style="vertical-align: middle; margin-left: .5rem"
value="#{customer.representative.name}"/>
</p:column>
<p:column field="date" headerText="Join Date" filterMatchMode="between">
<f:facet name="filter">
<p:datePicker selectionMode="range" onchange="PF('customersTable2').filter()"
styleClass="ui-custom-filter"/>
</f:facet>
</p:column>
<p:column field="status" headerText="Status" filterMatchMode="exact">
<f:facet name="filter">
<p:selectOneMenu onchange="PF('customersTable2').filter()" styleClass="ui-custom-filter">
<f:selectItem itemLabel="All" itemValue="#{null}" noSelectionOption="true"/>
<f:selectItems value="#{dtFilterView.customerStatus}"/>
</p:selectOneMenu>
</f:facet>
<span class="customer-badge status-#{customer.status.statusName}">#{customer.status.statusName}</span>
</p:column>
<p:column field="activity" headerText="Activity" sortBy="#{customer.activity}"
converter="jakarta.faces.Integer">
<p:progressBar value="#{customer.activity}" displayOnly="true" style="height: .5rem"/>
</p:column>
</p:dataTable>
</h:form>
</div>
<div class="card">
<h5>Global Filter Function</h5>
<h:form>
<p:dataTable var="customer" value="#{dtFilterView.customers3}" widgetVar="customersTable3"
emptyMessage="No customers found with given criteria"
filteredValue="#{dtFilterView.filteredCustomers3}"
globalFilterFunction="#{dtFilterView.globalFilterFunction}">
<f:facet name="header">
<div class="flex justify-content-end">
<p:inputText id="globalFilter" onkeyup="PF('customersTable3').filter()" style="width:300px"
placeholder="Search all fields using global function"/>
</div>
</f:facet>
<p:column field="name" filterable="false"/>
<p:column headerText="Country" sortBy="#{customer.country.name}">
<span class="flag flag-#{customer.country.code}" style="width: 30px; height: 20px"/>
<h:outputText style="vertical-align: middle; margin-left: .5rem"
value="#{customer.country.name}"/>
</p:column>
<p:column field="representative" headerText="Representative" filterable="false">
<p:graphicImage name="images/avatar/#{customer.representative.image}" library="demo" width="32"
style="vertical-align: middle"/>
<h:outputText style="vertical-align: middle; margin-left: .5rem"
value="#{customer.representative.name}"/>
</p:column>
<p:column field="date" filterable="false"/>
<p:column field="status" filterable="false">
<span class="customer-badge status-#{customer.status.statusName}">#{customer.status.statusName}</span>
</p:column>
<p:column headerText="Activity" sortBy="#{customer.activity}">
<p:progressBar value="#{customer.activity}" displayOnly="true" style="height: .5rem"/>
</p:column>
</p:dataTable>
</h:form>
</div>
package org.primefaces.showcase.view.data.datatable;
import jakarta.annotation.PostConstruct;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.model.FilterMeta;
import org.primefaces.model.MatchMode;
import org.primefaces.showcase.domain.Customer;
import org.primefaces.showcase.domain.CustomerStatus;
import org.primefaces.showcase.domain.Representative;
import org.primefaces.showcase.service.CustomerService;
import org.primefaces.util.LangUtils;
@Named("dtFilterView")
@ViewScoped
@RegisterForReflection(serialization = true)
public class FilterView implements Serializable {
@Inject
CustomerService service;
private List<Customer> customers1;
private List<Customer> customers2;
private List<Customer> customers3;
private List<Customer> filteredCustomers1;
private List<Customer> filteredCustomers2;
private List<Customer> filteredCustomers3;
private List<FilterMeta> filterBy;
private boolean globalFilterOnly;
@PostConstruct
public void init() {
globalFilterOnly = false;
customers1 = service.getCustomers(10);
customers2 = service.getCustomers(50);
customers3 = service.getCustomers(10);
filterBy = new ArrayList<>();
filterBy.add(FilterMeta.builder()
.field("status")
.filterValue(CustomerStatus.NEW)
.matchMode(MatchMode.EQUALS)
.build());
filterBy.add(FilterMeta.builder()
.field("date")
.filterValue(new ArrayList<>(Arrays.asList(LocalDate.now().minusDays(28), LocalDate.now().plusDays(28))))
.matchMode(MatchMode.BETWEEN)
.build());
}
public boolean globalFilterFunction(Object value, Object filter, Locale locale) {
String filterText = (filter == null) ? null : filter.toString().trim().toLowerCase();
if (LangUtils.isBlank(filterText)) {
return true;
}
int filterInt = getInteger(filterText);
Customer customer = (Customer) value;
return customer.getName().toLowerCase().contains(filterText)
|| customer.getCountry().getName().toLowerCase().contains(filterText)
|| customer.getRepresentative().getName().toLowerCase().contains(filterText)
|| customer.getDate().toString().toLowerCase().contains(filterText)
|| customer.getStatus().name().toLowerCase().contains(filterText)
|| customer.getActivity() < filterInt;
}
public void toggleGlobalFilter() {
setGlobalFilterOnly(!isGlobalFilterOnly());
}
private int getInteger(String string) {
try {
return Integer.parseInt(string);
} catch (NumberFormatException ex) {
return 0;
}
}
public List<Representative> getRepresentatives() {
return service.getRepresentatives();
}
public CustomerStatus[] getCustomerStatus() {
return service.getCustomerStatus();
}
public List<Customer> getCustomers1() {
return customers1;
}
public List<Customer> getCustomers2() {
return customers2;
}
public List<Customer> getCustomers3() {
return customers3;
}
public List<Customer> getFilteredCustomers1() {
return filteredCustomers1;
}
public void setFilteredCustomers1(List<Customer> filteredCustomers1) {
this.filteredCustomers1 = filteredCustomers1;
}
public List<Customer> getFilteredCustomers2() {
return filteredCustomers2;
}
public void setFilteredCustomers2(List<Customer> filteredCustomers2) {
this.filteredCustomers2 = filteredCustomers2;
}
public List<Customer> getFilteredCustomers3() {
return filteredCustomers3;
}
public void setFilteredCustomers3(List<Customer> filteredCustomers3) {
this.filteredCustomers3 = filteredCustomers3;
}
public void setService(CustomerService service) {
this.service = service;
}
public List<FilterMeta> getFilterBy() {
return filterBy;
}
public boolean isGlobalFilterOnly() {
return globalFilterOnly;
}
public void setGlobalFilterOnly(boolean globalFilterOnly) {
this.globalFilterOnly = globalFilterOnly;
}
}
package org.primefaces.showcase.service;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;
import org.primefaces.showcase.domain.*;
@Named
@ApplicationScoped
public class CustomerService {
List<Customer> customers;
private Country[] countries;
private Representative[] representatives;
private String[] names;
private String[] companies;
@PostConstruct
public void init() {
customers = new ArrayList<>();
countries = new Country[]{
new Country(0, "Argentina", "ar"),
new Country(1, "Australia", "au"),
new Country(2, "Brazil", "br"),
new Country(3, "Canada", "ca"),
new Country(4, "Germany", "de"),
new Country(5, "France", "fr"),
new Country(6, "India", "in"),
new Country(7, "Italy", "it"),
new Country(8, "Japan", "jp"),
new Country(9, "Russia", "ru"),
new Country(10, "Spain", "es"),
new Country(11, "United Kingdom", "gb")};
companies = new String[]{"Benton, John B Jr", "Chanay, Jeffrey A Esq", "Chemel, James L Cpa", "Feltz Printing Service",
"Printing Dimensions", "Chapman, Ross E Esq", "Morlong Associates", "Commercial Press", "Truhlar And Truhlar Attys",
"King, Christopher A Esq","Dorl, James J Esq","Rangoni Of Florence","Feiner Bros","Buckley Miller Wright","Rousseaux, Michael Esq",};
representatives = new Representative[]{new Representative("Amy Elsner", "amyelsner.png"), new Representative("Anna Fali", "annafali.png"),
new Representative("Asiya Javayant", "asiyajavayant.png"), new Representative("Bernardo Dominic", "bernardodominic.png"),
new Representative("Elwin Sharvill", "elwinsharvill.png"), new Representative("Ioni Bowcher", "ionibowcher.png"),
new Representative("Ivan Magalhaes", "ivanmagalhaes.png"), new Representative("Onyama Limba", "onyamalimba.png"),
new Representative("Stephen Shaw", "stephenshaw.png"), new Representative("Xuxue Feng", "xuxuefeng.png")};
names = new String[]{"James Butt", "David Darakjy", "Jeanfrancois Venere", "Ivar Paprocki", "Tony Foller",
"Adams Morasca", "Claire Tollner", "Costa Dilliard", "Juan Wieser", "Maria Marrier", "Jennifer Amigon",
"Stacey Maclead", "Leja Caldarera", "Morrow Ruta", "Arvin Albares", "Darci Poquette", "Izzy Garufi",
"Ricardo Gaucho", "Clifford Rim", "Emily Whobrey", "Kadeem Flosi", "Mujtaba Nicka", "Aika Inouye",
"Mayumi Kolmetz", "Misaki Royster", "Silvio Slusarski", "Nicolas Iturbide", "Antonio Caudy", """
Deepesh Chui\
""", "Aditya Kusko", "Aruna Figeroa", "Jones Vocelka", "Julie Stenseth", "Smith Glick",
"Johnson Sergi", "Francesco Shinko", "Salvatore Stockham", "Kaitlin Ostrosky", "Faith Gillian",
"Maisha Rulapaugh", "Jefferson Schemmer", "Leon Oldroyd", "Rodrigues Campain", "Alejandro Perin",
"Munro Ferencz", "Cody Saylors", "Chavez Briddick", "Sinclair Waycott", "Isabel Bowley", "Octavia Malet",
"Murillo Malet", "Greenwood Bolognia", "Wickens Nestle", "Ashley Doe"};
}
public List<Customer> getCustomers(int number) {
List<Customer> customers = new ArrayList<>();
for (int i = 0; i < number; i++) {
customers.add(new Customer(i + 1000, getName(), getCompany(), getCountry(), getDate(), CustomerStatus.random(), getActivity(), getRepresentative()));
}
return customers;
}
public List<Country> getCountries() {
return Arrays.asList(countries);
}
public CustomerStatus[] getCustomerStatus() {
return CustomerStatus.values();
}
public List<Representative> getRepresentatives() {
return Arrays.asList(representatives);
}
private String getName() {
return names[(int) (Math.random() * names.length)];
}
private Country getCountry() {
return countries[(int) (Math.random() * countries.length)];
}
private String getCompany() {
return companies[(int) (Math.random() * companies.length)];
}
private LocalDate getDate() {
LocalDate now = LocalDate.now();
long randomDay = ThreadLocalRandom.current().nextLong(now.minusDays(30).toEpochDay(), now.toEpochDay());
return LocalDate.ofEpochDay(randomDay);
}
private int getActivity() {
return (int) (Math.random() * 100);
}
private Representative getRepresentative() {
return representatives[(int) (Math.random() * representatives.length)];
}
}
package org.primefaces.showcase.domain;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Objects;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String company;
private Country country;
private LocalDate date;
private CustomerStatus status;
private int activity;
private Representative representative;
public Customer() {
}
public Customer(int id, String name, String company, Country country, LocalDate date, CustomerStatus status, int activity,
Representative representative) {
this.id = id;
this.name = name;
this.company = company;
this.country = country;
this.date = date;
this.status = status;
this.activity = activity;
this.representative = representative;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public CustomerStatus getStatus() {
return status;
}
public void setStatus(CustomerStatus status) {
this.status = status;
}
public int getActivity() {
return activity;
}
public void setActivity(int activity) {
this.activity = activity;
}
public Representative getRepresentative() {
return representative;
}
public void setRepresentative(Representative representative) {
this.representative = representative;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Customer customer = (Customer) o;
return id == customer.id
&& activity == customer.activity
&& Objects.equals(name, customer.name)
&& Objects.equals(company, customer.company)
&& Objects.equals(country, customer.country)
&& Objects.equals(date, customer.date)
&& status == customer.status
&& Objects.equals(representative, customer.representative);
}
@Override
public int hashCode() {
return Objects.hash(id, name, company, country, date, status, activity, representative);
}
}
package org.primefaces.showcase.domain;
import java.io.Serializable;
import java.util.Locale;
import java.util.Objects;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class Country implements Serializable, Comparable<Country> {
private int id;
private String name;
private String code;
private Locale locale;
private boolean rtl;
public Country() {
}
public Country(int id, Locale locale) {
this(id, locale.getDisplayCountry(), locale.getCountry().toLowerCase(), locale);
}
public Country(int id, Locale locale, boolean rtl) {
this(id, locale.getDisplayCountry(), locale.getCountry().toLowerCase(), locale);
this.rtl = rtl;
}
public Country(int id, String name, String code) {
this(id, name, code, null);
}
public Country(int id, String name, String code, Locale locale) {
this.id = id;
this.name = name;
this.code = code;
this.locale = locale;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
public String getLanguage() {
return locale == null ? "en" : locale.getLanguage();
}
public String getDisplayLanguage() {
return locale == null ? "English" : locale.getDisplayLanguage();
}
public boolean isRtl() {
return rtl;
}
public void setRtl(boolean rtl) {
this.rtl = rtl;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Country country = (Country) o;
return id == country.id
&& Objects.equals(name, country.name)
&& Objects.equals(code, country.code);
}
@Override
public int hashCode() {
return Objects.hash(id, name, code);
}
@Override
public String toString() {
return name;
}
@Override
public int compareTo(Country o) {
return name.compareTo(o.name);
}
}
package org.primefaces.showcase.domain;
import java.util.Random;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public enum CustomerStatus {
QUALIFIED,
UNQUALIFIED,
NEGOTIATION,
NEW,
RENEWAL,
PROPOSAL;
public static CustomerStatus random() {
Random random = new Random();
return values()[random.nextInt(values().length)];
}
public String getStatusName() {
return name();
}
}
package org.primefaces.showcase.domain;
import java.io.Serializable;
import java.util.Objects;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class Representative implements Serializable, Comparable<Representative> {
private static final long serialVersionUID = 1L;
private String name;
private String image;
public Representative() {
}
public Representative(String name, String image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Representative that = (Representative) o;
return Objects.equals(name, that.name)
&& Objects.equals(image, that.image);
}
@Override
public int hashCode() {
return Objects.hash(name, image);
}
@Override
public String toString() {
return name;
}
@Override
public int compareTo(Representative o) {
return name.compareTo(o.name);
}
}