SelectManyCheckbox is used to choose multiple items from a collection.
<style>
.grid-checkbox td {
padding: .5rem;
}
</style>
<div class="card">
<h:form>
<p:growl>
<p:autoUpdate />
</p:growl>
<h5 style="margin-top:0px">Basic Layout</h5>
<p:selectManyCheckbox id="basicAjax" value="#{checkboxView.selectedOptions}" widgetVar="blub">
<p:ajax listener="#{checkboxView.selectedOptionsChanged}" />
<f:selectItem itemLabel="Option 1" itemValue="Option 1"/>
<f:selectItem itemLabel="Option 2" itemValue="Option 2"/>
<f:selectItem itemLabel="Option 3" itemValue="Option 3"/>
</p:selectManyCheckbox>
<h5>Responsive</h5>
<p:selectManyCheckbox id="grid2" value="#{checkboxView.selectedCities2}" layout="responsive"
columns="3">
<f:selectItems value="#{checkboxView.cities}" var="city" itemLabel="#{city}" itemValue="#{city}"/>
</p:selectManyCheckbox>
<h5>Grid Layout (deprecated)</h5>
<p:selectManyCheckbox id="grid" value="#{checkboxView.selectedCities}" layout="grid" columns="3" styleClass="grid-checkbox">
<f:selectItems value="#{checkboxView.cities}" var="city" itemLabel="#{city}" itemValue="#{city}"/>
</p:selectManyCheckbox>
<h5>Line Direction</h5>
<p:selectManyCheckbox id="lineDirection" value="#{checkboxView.selectedCities}" layout="lineDirection">
<f:selectItems value="#{checkboxView.cities}" var="city" itemLabel="#{city}" itemValue="#{city}"/>
</p:selectManyCheckbox>
<h5>Page Direction</h5>
<p:selectManyCheckbox id="pageDirection" value="#{checkboxView.selectedCities}" layout="pageDirection">
<f:selectItems value="#{checkboxView.cities}" var="city" itemLabel="#{city}" itemValue="#{city}"/>
</p:selectManyCheckbox>
<h5>Custom Layout</h5>
<p:outputPanel id="customPanel">
<p:selectManyCheckbox id="custom" value="#{checkboxView.selectedOptions2}" layout="custom">
<p:ajax listener="#{checkboxView.selectedOptions2Changed}" />
<f:selectItem itemLabel="Option 1" itemValue="Option 1"/>
<f:selectItem itemLabel="Option 2" itemValue="Option 2"/>
<f:selectItem itemLabel="Option 3" itemValue="Option 3"/>
</p:selectManyCheckbox>
<div class="grid text-center">
<div class="col">
<p:outputLabel for="opt1" value="Option 1" styleClass="block mb-2"/>
<p:checkbox id="opt1" for="custom" itemIndex="0"/>
</div>
<div class="col">
<p:outputLabel for="opt2" value="Option 2" styleClass="block mb-2"/>
<p:checkbox id="opt2" for="custom" itemIndex="1"/>
</div>
<div class="col">
<p:outputLabel for="opt3" value="Option 3" styleClass="block mb-2"/>
<p:checkbox id="opt3" for="custom" itemIndex="2"/>
</div>
</div>
</p:outputPanel>
<h5>Grouped</h5>
<p:selectManyCheckbox id="grid3" value="#{checkboxView.selectedCountries}" layout="pageDirection"
columns="1" styleClass="grid-checkbox">
<f:selectItems value="#{checkboxView.countries}" var="country" itemLabel="#{country}"
itemValue="#{country}"/>
</p:selectManyCheckbox>
</h:form>
</div>
package org.primefaces.showcase.view.input;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.faces.model.SelectItem;
import jakarta.faces.model.SelectItemGroup;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.util.ArrayList;
import java.util.List;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.primefaces.showcase.domain.Country;
import org.primefaces.showcase.service.CountryService;
@Named
@RequestScoped
@RegisterForReflection(serialization = true)
public class CheckboxView {
@Inject
CountryService service;
private String[] selectedOptions;
private String[] selectedOptions2;
private String[] selectedCities;
private String[] selectedCities2;
private List<String> cities;
private List<SelectItem> countries;
private String[] selectedCountries;
private List<Country> countries2;
private List<Country> selectedCountries2;
private List<SelectItem> countries3;
private List<Country> selectedCountries3;
@PostConstruct
public void init() {
cities = new ArrayList<>();
cities.add("Miami");
cities.add("London");
cities.add("Paris");
cities.add("Istanbul");
cities.add("Berlin");
cities.add("Barcelona");
cities.add("Rome");
cities.add("Brasilia");
cities.add("Amsterdam");
countries = new ArrayList<>();
SelectItemGroup europeCountries = new SelectItemGroup("European Countries");
europeCountries.setSelectItems(new SelectItem("Germany", "Germany"),
new SelectItem("Turkey", "Turkey"),
new SelectItem("Spain", "Spain"));
SelectItemGroup americaCountries = new SelectItemGroup("American Countries");
americaCountries.setSelectItems(new SelectItem("United States", "United States"),
new SelectItem("Brazil", "Brazil"),
new SelectItem("Mexico", "Mexico"));
countries.add(europeCountries);
countries.add(americaCountries);
countries2 = service.getCountries();
countries3 = new ArrayList<>();
SelectItemGroup europeCountries3 = new SelectItemGroup("European Countries");
europeCountries3.setSelectItems(isoCodesToSelectItemArray("DE", "TR", "ES"));
SelectItemGroup americaCountries3 = new SelectItemGroup("American Countries");
americaCountries3.setSelectItems(isoCodesToSelectItemArray("US", "BR", "MX"));
countries3.add(europeCountries3);
countries3.add(americaCountries3);
}
public String[] getSelectedOptions() {
return selectedOptions;
}
public void setSelectedOptions(String[] selectedOptions) {
this.selectedOptions = selectedOptions;
}
public String[] getSelectedOptions2() {
return selectedOptions2;
}
public void setSelectedOptions2(String[] selectedOptions2) {
this.selectedOptions2 = selectedOptions2;
}
public String[] getSelectedCities() {
return selectedCities;
}
public void setSelectedCities(String[] selectedCities) {
this.selectedCities = selectedCities;
}
public String[] getSelectedCities2() {
return selectedCities2;
}
public void setSelectedCities2(String[] selectedCities2) {
this.selectedCities2 = selectedCities2;
}
public List<String> getCities() {
return cities;
}
public void setCities(List<String> cities) {
this.cities = cities;
}
public List<SelectItem> getCountries() {
return countries;
}
public void setCountries(List<SelectItem> countries) {
this.countries = countries;
}
public String[] getSelectedCountries() {
return selectedCountries;
}
public void setSelectedCountries(String[] selectedCountries) {
this.selectedCountries = selectedCountries;
}
public List<Country> getCountries2() {
return countries2;
}
public void setCountries2(List<Country> countries2) {
this.countries2 = countries2;
}
public List<Country> getSelectedCountries2() {
return selectedCountries2;
}
public void setSelectedCountries2(List<Country> selectedCountries2) {
this.selectedCountries2 = selectedCountries2;
}
public List<SelectItem> getCountries3() {
return countries3;
}
public void setCountries3(List<SelectItem> countries3) {
this.countries3 = countries3;
}
public List<Country> getSelectedCountries3() {
return selectedCountries3;
}
public void setSelectedCountries3(List<Country> selectedCountries3) {
this.selectedCountries3 = selectedCountries3;
}
public void onItemSelect(SelectEvent event) {
FacesMessage msg = new FacesMessage();
msg.setSummary("Item selected: " + event.getObject().toString());
msg.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onItemUnselect(UnselectEvent event) {
FacesMessage msg = new FacesMessage();
msg.setSummary("Item unselected: " + event.getObject().toString());
msg.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void selectedOptionsChanged() {
String message = "selectedOptions changed to: ";
if (selectedOptions != null) {
for (int i = 0; i < selectedOptions.length; i++) {
if (i > 0) {
message += ", ";
}
message += selectedOptions[i];
}
}
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
}
public void selectedOptions2Changed() {
String message = "selectedOptions2 changed to: ";
if (selectedOptions2 != null) {
for (int i = 0; i < selectedOptions2.length; i++) {
if (i > 0) {
message += ", ";
}
message += selectedOptions2[i];
}
}
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
}
private SelectItem[] isoCodesToSelectItemArray(String... isoCodes) {
return CountryService.toCountryStream(isoCodes)
.map(country -> new SelectItem(country, country.getName()))
.toArray(SelectItem[]::new);
}
}