MultiSelectListbox is used to select an item from a collection of listboxes that are in parent-child relationship.
<div class="card">
<p:multiSelectListbox value="#{multiSelectView.selection}" effect="slide" header="Categories"
showHeaders="true">
<f:selectItems value="#{multiSelectView.categories}"/>
</p:multiSelectListbox>
</div>
package org.primefaces.showcase.view.input;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.model.SelectItem;
import jakarta.faces.model.SelectItemGroup;
import jakarta.inject.Named;
import java.util.ArrayList;
import java.util.List;
import io.quarkus.runtime.annotations.RegisterForReflection;
@Named
@RequestScoped
@RegisterForReflection(serialization = true)
public class MultiSelectView {
private List<SelectItem> categories;
private String selection;
@PostConstruct
public void init() {
categories = new ArrayList<>();
SelectItemGroup group1 = new SelectItemGroup("Group 1");
SelectItemGroup group2 = new SelectItemGroup("Group 2");
SelectItemGroup group3 = new SelectItemGroup("Group 3");
SelectItemGroup group4 = new SelectItemGroup("Group 4");
SelectItemGroup group11 = new SelectItemGroup("Group 1.1");
SelectItemGroup group12 = new SelectItemGroup("Group 1.2");
SelectItemGroup group21 = new SelectItemGroup("Group 2.1");
SelectItem option31 = new SelectItem("Option 3.1", "Option 3.1");
SelectItem option32 = new SelectItem("Option 3.2", "Option 3.2");
SelectItem option33 = new SelectItem("Option 3.3", "Option 3.3");
SelectItem option34 = new SelectItem("Option 3.4", "Option 3.4");
SelectItem option41 = new SelectItem("Option 4.1", "Option 4.1");
SelectItem option111 = new SelectItem("Option 1.1.1");
SelectItem option112 = new SelectItem("Option 1.1.2");
group11.setSelectItems(option111, option112);
SelectItem option121 = new SelectItem("Option 1.2.1", "Option 1.2.1");
SelectItem option122 = new SelectItem("Option 1.2.2", "Option 1.2.2");
SelectItem option123 = new SelectItem("Option 1.2.3", "Option 1.2.3");
group12.setSelectItems(option121, option122, option123);
SelectItem option211 = new SelectItem("Option 2.1.1", "Option 2.1.1");
group21.setSelectItems(option211);
group1.setSelectItems(group11, group12);
group2.setSelectItems(group21);
group3.setSelectItems(option31, option32, option33, option34);
group4.setSelectItems(option41);
categories.add(group1);
categories.add(group2);
categories.add(group3);
categories.add(group4);
}
public List<SelectItem> getCategories() {
return categories;
}
public String getSelection() {
return selection;
}
public void setSelection(String selection) {
this.selection = selection;
}
}