AutoComplete displays suggestions while the input is being type. AutoComplete features various options, customizable content, multiple selection, effects and events.
<div class="card ui-fluid ">
<h:form>
<p:growl id="msgs" showDetail="true" skipDetailIfEqualsSummary="true"/>
<h5 class="mt-0">Single</h5>
<div class="formgrid grid">
<div class="field col-12 md:col-4">
<p:outputLabel value="Simple" for="@next"/>
<p:autoComplete id="acSimple" value="#{autoCompleteView.txt1}"
completeMethod="#{autoCompleteView.completeText}" scrollHeight="250"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Min Length (3)" for="@next"/>
<p:autoComplete id="acMinLength" minQueryLength="3" value="#{autoCompleteView.txt2}"
completeMethod="#{autoCompleteView.completeText}" effect="fade"
scrollHeight="250"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Delay(1000)" for="@next"/>
<p:autoComplete id="acDelay" queryDelay="1000" value="#{autoCompleteView.txt3}"
completeMethod="#{autoCompleteView.completeText}" effect="blind"
scrollHeight="250"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Max Results(5)" for="@next"/>
<p:autoComplete id="acMaxResults" maxResults="5" value="#{autoCompleteView.txt4}"
completeMethod="#{autoCompleteView.completeText}" scrollHeight="250"
moreText="more items available">
<p:ajax event="moreTextSelect" listener="#{autoCompleteView.onMoreTextSelect}"
update="msgs"/>
</p:autoComplete>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Force Selection" for="@next"/>
<p:autoComplete id="acForce" forceSelection="true" value="#{autoCompleteView.txt5}"
completeMethod="#{autoCompleteView.completeText}" scrollHeight="250"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="DropDown" for="@next"/>
<p:autoComplete id="dd" dropdown="true" value="#{autoCompleteView.txt6}"
completeMethod="#{autoCompleteView.completeText}" scrollHeight="250"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Cache" for="@next"/>
<p:autoComplete id="cache" cache="true" cacheTimeout="30000" value="#{autoCompleteView.txt7}"
completeMethod="#{autoCompleteView.completeText}" scrollHeight="250"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Select Event" for="@next"/>
<p:autoComplete id="event" value="#{autoCompleteView.txt8}"
completeMethod="#{autoCompleteView.completeText}" scrollHeight="250">
<p:ajax event="itemSelect" listener="#{autoCompleteView.onItemSelect}" update="msgs"/>
</p:autoComplete>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Empty message" for="@next"/>
<p:autoComplete id="emptyMessage" value="#{autoCompleteView.txt9}"
completeMethod="#{autoCompleteView.noResults}"
emptyMessage="No results" scrollHeight="250">
<p:ajax event="emptyMessageSelect" listener="#{autoCompleteView.onEmptyMessageSelect}"
update="msgs"/>
</p:autoComplete>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Pojo" for="@next"/>
<p:autoComplete id="pojo" value="#{autoCompleteView.country1}"
completeMethod="#{autoCompleteView.completeCountry}"
var="country" itemLabel="#{country.name}" itemValue="#{country}"
converter="#{countryConverter}" forceSelection="true" scrollHeight="250"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Pojo (via REST)" for="pojoRest"/>
<p:autoComplete id="pojoRest" widgetVar="countryPojoRest" value="#{autoCompleteView.country5}"
var="country" itemLabel="#{country.displayName}" itemValue="#{country}"
converter="#{countryConverter}"
completeEndpoint="#{request.contextPath}/rest/country/autocomplete"
forceSelection="true" emptyMessage="sorry, no suggestions"
scrollHeight="250"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="LazyModel" for="lazyModel"/>
<p:autoComplete id="lazyModel" widgetVar="lazyModel" value="#{autoCompleteView.txt11}"
var="customer" itemLabel="#{customer.name}" itemValue="#{customer.name}"
lazyModel="#{autoCompleteView.lazyModel}" lazyField="name"
forceSelection="true" emptyMessage="sorry, no suggestions"
maxResults="10"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Custom Content" for="@next"/>
<p:autoComplete id="countryCustom" value="#{autoCompleteView.country2}"
completeMethod="#{autoCompleteView.completeCountry}"
var="country" itemLabel="#{country.name}" itemValue="#{country}"
converter="#{countryConverter}" forceSelection="true" scrollHeight="250">
<p:column>
<span class="flag flag-#{country.code}" style="width: 30px; height: 20px"/>
<h:outputText style="vertical-align: middle; margin-left: .5rem"
value="#{country.name}"/>
</p:column>
</p:autoComplete>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Itemtip" for="@next"/>
<p:autoComplete id="itemTip" value="#{autoCompleteView.country3}"
completeMethod="#{autoCompleteView.completeCountry}"
var="country" itemLabel="#{country.name}" itemValue="#{country}"
converter="#{countryConverter}" scrollHeight="250">
<f:facet name="itemtip">
<h:panelGrid columns="2" cellpadding="5">
<f:facet name="header">
<span class="flag flag-#{country.code}" style="width: 30px; height: 20px"/>
</f:facet>
<h:outputText value="Name"/>
<h:outputText value="#{country.name}"/>
<h:outputText value="Code"/>
<h:outputText value="#{country.code}"/>
</h:panelGrid>
</f:facet>
</p:autoComplete>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Group" for="@next"/>
<p:autoComplete id="group" value="#{autoCompleteView.country4}"
completeMethod="#{autoCompleteView.completeCountry}"
var="country" itemLabel="#{country.name}" itemValue="#{country}"
converter="#{countryConverter}" forceSelection="true"
groupBy="#{autoCompleteView.getCountryGroup(country)}" escape="false"
scrollHeight="250"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel value="Footer" for="@next"/>
<p:autoComplete id="acFooter" value="#{autoCompleteView.txt10}"
completeMethod="#{autoCompleteView.completeText}"
maxResults="10" emptyMessage="No results">
<f:facet name="footer">
<div class="ui-fluid" style="padding:0.5rem 1rem 1rem 1rem">
<p:button value="Add new" onclick="alert('This could open a dialog')"/>
</div>
</f:facet>
</p:autoComplete>
</div>
</div>
<h5>Multiple</h5>
<p:autoComplete id="countries" multiple="true" value="#{autoCompleteView.selectedCountries}"
completeMethod="#{autoCompleteView.completeCountry}"
var="country" itemLabel="#{country.name}" itemValue="#{country}"
converter="#{countryConverter}" forceSelection="true" scrollHeight="250">
<p:column>
<span class="flag flag-#{country.code}" style="width: 30px; height: 20px"/>
<h:outputText style="vertical-align: middle; margin-left: .5rem" value="#{country.name}"/>
</p:column>
</p:autoComplete>
</h:form>
</div>
package org.primefaces.showcase.view.input;
import jakarta.annotation.PostConstruct;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.showcase.domain.Country;
import org.primefaces.showcase.domain.Customer;
import org.primefaces.showcase.service.CountryService;
import org.primefaces.showcase.service.CustomerService;
import org.primefaces.showcase.view.data.datatable.LazyCustomerDataModel;
@Named
@ViewScoped
@RegisterForReflection(serialization = true)
public class AutoCompleteView implements Serializable {
@Inject
CountryService countryService;
@Inject
CustomerService service;
private String txt1;
private String txt2;
private String txt3;
private String txt4;
private String txt5;
private String txt6;
private String txt7;
private String txt8;
private String txt9;
private String txt10;
private String txt11;
private Country country1;
private Country country2;
private Country country3;
private Country country4;
private Country country5;
private List<Country> selectedCountries;
private LazyDataModel<Customer> lazyModel;
@PostConstruct
public void init() {
lazyModel = new LazyCustomerDataModel(service.getCustomers(200));
}
public List<String> completeText(String query) {
String queryLowerCase = query.toLowerCase();
List<String> countryList = new ArrayList<>();
List<Country> countries = countryService.getCountries();
for (Country country : countries) {
countryList.add(country.getName());
}
return countryList.stream().filter(t -> t.toLowerCase().startsWith(queryLowerCase)).collect(Collectors.toList());
}
public List<String> noResults(String query) {
return Collections.emptyList();
}
public List<Country> completeCountry(String query) {
String queryLowerCase = query.toLowerCase();
List<Country> countries = countryService.getCountries();
return countries.stream().filter(t -> t.getName().toLowerCase().contains(queryLowerCase)).collect(Collectors.toList());
}
public void onItemSelect(SelectEvent<String> event) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Country Selected", event.getObject()));
}
public void onEmptyMessageSelect() {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Empty message selected"));
}
public void onMoreTextSelect() {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("More text selected"));
}
public String getTxt1() {
return txt1;
}
public void setTxt1(String txt1) {
this.txt1 = txt1;
}
public String getTxt2() {
return txt2;
}
public void setTxt2(String txt2) {
this.txt2 = txt2;
}
public String getTxt3() {
return txt3;
}
public void setTxt3(String txt3) {
this.txt3 = txt3;
}
public String getTxt4() {
return txt4;
}
public void setTxt4(String txt4) {
this.txt4 = txt4;
}
public String getTxt5() {
return txt5;
}
public void setTxt5(String txt5) {
this.txt5 = txt5;
}
public String getTxt6() {
return txt6;
}
public void setTxt6(String txt6) {
this.txt6 = txt6;
}
public String getTxt7() {
return txt7;
}
public void setTxt7(String txt7) {
this.txt7 = txt7;
}
public String getTxt8() {
return txt8;
}
public void setTxt8(String txt8) {
this.txt8 = txt8;
}
public String getTxt9() {
return txt9;
}
public void setTxt9(String txt9) {
this.txt9 = txt9;
}
public String getTxt10() {
return txt10;
}
public void setTxt10(String txt10) {
this.txt10 = txt10;
}
public String getTxt11() {
return txt11;
}
public void setTxt11(String txt11) {
this.txt11 = txt11;
}
public Country getCountry1() {
return country1;
}
public void setCountry1(Country country1) {
this.country1 = country1;
}
public Country getCountry2() {
return country2;
}
public void setCountry2(Country country2) {
this.country2 = country2;
}
public Country getCountry3() {
return country3;
}
public void setCountry3(Country country3) {
this.country3 = country3;
}
public Country getCountry4() {
return country4;
}
public void setCountry4(Country country4) {
this.country4 = country4;
}
public Country getCountry5() {
return country5;
}
public void setCountry5(Country country5) {
this.country5 = country5;
}
public List<Country> getSelectedCountries() {
return selectedCountries;
}
public void setSelectedCountries(List<Country> selectedCountries) {
this.selectedCountries = selectedCountries;
}
public void setCountryService(CountryService countryService) {
this.countryService = countryService;
}
public char getCountryGroup(Country country) {
return country.getName().charAt(0);
}
public LazyDataModel<Customer> getLazyModel() {
return lazyModel;
}
}
package org.primefaces.showcase.service;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;
import org.primefaces.showcase.domain.Country;
@Named
@ApplicationScoped
public class CountryService {
private List<Country> countries;
private Map<Integer, Country> countriesAsMap;
private List<Country> locales;
private Map<Integer, Country> localesAsMap;
public static Stream<Country> toCountryStream(String... isoCodes) {
return Stream.of(isoCodes)
.map(isoCode -> new Locale("", isoCode))
.map(CountryService::toCountry);
}
public static Country toCountry(Locale locale) {
return CountryService.toCountry(locale, false);
}
public static Country toCountry(Locale locale, boolean rtl) {
//use hash code from locale to have a reproducible ID (required for CountryConverter)
return new Country(locale.hashCode(), locale, rtl);
}
@PostConstruct
public void init() {
countries = CountryService.toCountryStream(Locale.getISOCountries())
.sorted(Comparator.comparing(Country::getName))
.collect(Collectors.toList());
locales = new ArrayList<>();
locales.add(CountryService.toCountry(Locale.US));
locales.add(CountryService.toCountry(Locale.UK));
locales.add(CountryService.toCountry(new Locale("en", "AU")));
locales.add(CountryService.toCountry(Locale.FRANCE));
locales.add(CountryService.toCountry(Locale.GERMANY));
locales.add(CountryService.toCountry(new Locale("de", "AT")));
locales.add(CountryService.toCountry(Locale.ITALY));
locales.add(CountryService.toCountry(Locale.KOREA));
locales.add(CountryService.toCountry(new Locale("es", "ES")));
locales.add(CountryService.toCountry(new Locale("ca", "ES")));
locales.add(CountryService.toCountry(new Locale("nl", "NL")));
locales.add(CountryService.toCountry(new Locale("pt", "BR")));
locales.add(CountryService.toCountry(new Locale("pt", "PT")));
locales.add(CountryService.toCountry(new Locale("ar", "SA"), true));
locales.add(CountryService.toCountry(new Locale("ar", "TN"), true));
locales.add(CountryService.toCountry(new Locale("bg", "BG")));
locales.add(CountryService.toCountry(new Locale("cs", "CZ")));
locales.add(CountryService.toCountry(new Locale("el", "GR")));
locales.add(CountryService.toCountry(new Locale("fa", "IR"), true));
locales.add(CountryService.toCountry(new Locale("fi", "FI")));
locales.add(CountryService.toCountry(new Locale("da", "DK")));
locales.add(CountryService.toCountry(new Locale("hi", "IN")));
locales.add(CountryService.toCountry(new Locale("in", "ID")));
locales.add(CountryService.toCountry(new Locale("hr", "HR")));
locales.add(CountryService.toCountry(new Locale("ja", "JP")));
locales.add(CountryService.toCountry(new Locale("hu", "HU")));
locales.add(CountryService.toCountry(new Locale("he", "IL"), true));
locales.add(CountryService.toCountry(new Locale("ka", "GE")));
locales.add(CountryService.toCountry(new Locale("ckb", "IQ"), true));
locales.add(CountryService.toCountry(new Locale("km", "KH")));
locales.add(CountryService.toCountry(new Locale("ky", "KG")));
locales.add(CountryService.toCountry(new Locale("kk", "KZ")));
locales.add(CountryService.toCountry(new Locale("lt", "LT")));
locales.add(CountryService.toCountry(new Locale("lv", "LV")));
locales.add(CountryService.toCountry(new Locale("ms", "MY")));
locales.add(CountryService.toCountry(new Locale("no", "NO")));
locales.add(CountryService.toCountry(new Locale("pl", "PL")));
locales.add(CountryService.toCountry(new Locale("ro", "RO")));
locales.add(CountryService.toCountry(new Locale("ru", "RU")));
locales.add(CountryService.toCountry(new Locale("sk", "SK")));
locales.add(CountryService.toCountry(new Locale("sl", "SI")));
locales.add(CountryService.toCountry(new Locale("sr", "RS")));
locales.add(CountryService.toCountry(new Locale("sv", "SE")));
locales.add(CountryService.toCountry(new Locale("th", "TH")));
locales.add(CountryService.toCountry(new Locale("tr", "TR")));
locales.add(CountryService.toCountry(new Locale("uk", "UA")));
locales.add(CountryService.toCountry(new Locale("vi", "VN")));
locales.add(CountryService.toCountry(Locale.SIMPLIFIED_CHINESE));
locales.add(CountryService.toCountry(Locale.TRADITIONAL_CHINESE));
}
public List<Country> getCountries() {
return new ArrayList<>(countries);
}
public Map<Integer, Country> getCountriesAsMap() {
if (countriesAsMap == null) {
countriesAsMap = getCountries().stream().collect(Collectors.toMap(Country::getId, country -> country));
}
return countriesAsMap;
}
public List<Country> getLocales() {
return new ArrayList<>(locales);
}
public Map<Integer, Country> getLocalesAsMap() {
if (localesAsMap == null) {
localesAsMap = getLocales().stream().collect(Collectors.toMap(Country::getId, country -> country));
}
return localesAsMap;
}
}
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.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.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);
}
}
package org.primefaces.showcase.convert;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.convert.Converter;
import jakarta.faces.convert.ConverterException;
import jakarta.faces.convert.FacesConverter;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.primefaces.showcase.domain.Country;
import org.primefaces.showcase.service.CountryService;
@Named
@ApplicationScoped
@FacesConverter(value = "countryConverter", managed = true)
public class CountryConverter implements Converter<Country> {
@Inject
CountryService countryService;
@Override
public Country getAsObject(FacesContext context, UIComponent component, String value) {
if (value != null && value.trim().length() > 0) {
try {
return countryService.getCountriesAsMap().get(Integer.parseInt(value));
} catch (NumberFormatException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid country."));
}
} else {
return null;
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Country value) {
if (value != null) {
return String.valueOf(value.getId());
} else {
return null;
}
}
}
package org.primefaces.showcase.view.data.datatable;
import java.beans.IntrospectionException;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import jakarta.faces.context.FacesContext;
import org.apache.commons.collections4.ComparatorUtils;
import org.primefaces.model.FilterMeta;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import org.primefaces.model.filter.FilterConstraint;
import org.primefaces.showcase.domain.Customer;
import org.primefaces.showcase.util.ShowcaseUtil;
import org.primefaces.util.LocaleUtils;
/**
* Dummy implementation of LazyDataModel that uses a list to mimic a real datasource like a database.
*/
public class LazyCustomerDataModel extends LazyDataModel<Customer> {
private static final long serialVersionUID = 1L;
private List<Customer> datasource;
public LazyCustomerDataModel(List<Customer> datasource) {
this.datasource = datasource;
}
@Override
public Customer getRowData(String rowKey) {
for (Customer customer : datasource) {
if (customer.getId() == Integer.parseInt(rowKey)) {
return customer;
}
}
return null;
}
@Override
public String getRowKey(Customer customer) {
return String.valueOf(customer.getId());
}
@Override
public int count(Map<String, FilterMeta> filterBy) {
return (int) datasource.stream()
.filter(o -> filter(FacesContext.getCurrentInstance(), filterBy.values(), o))
.count();
}
@Override
public List<Customer> load(int offset, int pageSize, Map<String, SortMeta> sortBy, Map<String, FilterMeta> filterBy) {
// apply offset & filters
List<Customer> customers = datasource.stream()
.filter(o -> filter(FacesContext.getCurrentInstance(), filterBy.values(), o))
.collect(Collectors.toList());
// sort
if (!sortBy.isEmpty()) {
List<Comparator<Customer>> comparators = sortBy.values().stream()
.map(o -> new LazySorter(o.getField(), o.getOrder()))
.collect(Collectors.toList());
Comparator<Customer> cp = ComparatorUtils.chainedComparator(comparators); // from apache
customers.sort(cp);
}
return customers.subList(offset, Math.min(offset + pageSize, customers.size()));
}
private boolean filter(FacesContext context, Collection<FilterMeta> filterBy, Object o) {
boolean matching = true;
for (FilterMeta filter : filterBy) {
FilterConstraint constraint = filter.getConstraint();
Object filterValue = filter.getFilterValue();
try {
Object columnValue = String.valueOf(ShowcaseUtil.getPropertyValueViaReflection(o, filter.getField()));
matching = constraint.isMatching(context, columnValue, filterValue, LocaleUtils.getCurrentLocale());
}
catch (ReflectiveOperationException | IntrospectionException e) {
matching = false;
}
if (!matching) {
break;
}
}
return matching;
}
}
package org.primefaces.showcase.rest;
import java.util.List;
import java.util.stream.Collectors;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import org.primefaces.model.rest.AutoCompleteSuggestion;
import org.primefaces.model.rest.AutoCompleteSuggestionResponse;
import org.primefaces.showcase.domain.Country;
@Path("/country")
public class CountryRestService {
@Inject
org.primefaces.showcase.service.CountryService service;
@GET
@Path("/autocomplete")
@Produces({MediaType.APPLICATION_JSON})
public AutoCompleteSuggestionResponse autocomplete(@QueryParam("query") String query) {
String queryLowerCase = query.toLowerCase();
List<Country> allCountrys = service.getCountries();
return new AutoCompleteSuggestionResponse(allCountrys.stream()
.filter(t -> t.getName().toLowerCase().contains(queryLowerCase))
.map(t -> new AutoCompleteSuggestion(Integer.toString(t.getId()), t.getName()))
.collect(Collectors.toList()));
}
}