The rich:select component is designed to replace standard h:selectOneMenu. It adds various features and options including core Ajax functionality, and skinning. Some of the notable features are:
This example shows a select component that behaves basically the same as the standard:
In the next example you can manually enter values, and any invalid entries are not allowed.
In the next example the items list is fetched dynamically as in the autocomplete component.
<!DOCTYPE html> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"> <h:form id="form"> <p>This example shows a select component that behaves basically the same as the standard:</p> <rich:panel style="width:220px;"> <f:facet name="header"> <h:outputText value="Simples select" /> </f:facet> <rich:select> <f:selectItem itemValue="0" itemLabel="Option 1" /> <f:selectItem itemValue="1" itemLabel="Option 2" /> <f:selectItem itemValue="2" itemLabel="Option 3" /> <f:selectItem itemValue="3" itemLabel="Option 4" /> <f:selectItem itemValue="4" itemLabel="Option 5" /> </rich:select> </rich:panel> <p>In the next example you can manually enter values, and any invalid entries are not allowed.</p> <rich:panel style="width:220px;"> <f:facet name="header"> <h:outputText value="Select with manual input" /> </f:facet> <rich:select enableManualInput="true" defaultLabel="start typing for select"> <f:selectItems value="#{inplaceSelectBean.capitalsOptions}" /> </rich:select> </rich:panel> <p>In the next example the items list is fetched dynamically as in the autocomplete component.</p> <rich:panel style="width:220px;"> <rich:select mode="cachedAjax" minChars="1" autocompleteMethod="#{inplaceSelectBean.autocomplete}" var='capital' itemValue="${capital}" itemLabel="#{capital.state}"/> </rich:panel> </h:form> </ui:composition>
package org.richfaces.demo.inplaces; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.RequestScoped; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.model.SelectItem; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import org.richfaces.demo.tables.model.capitals.Capital; @ManagedBean @RequestScoped public class InplaceSelectBean { @ManagedProperty(value = "#{capitalsParser.capitalsList}") private List<Capital> capitals; private List<SelectItem> capitalsOptions = null; private String value; @PostConstruct public void init() { capitalsOptions = new ArrayList<SelectItem>(); for (Capital capital : capitals) { capitalsOptions.add(new SelectItem(capital.getName(), capital.getState())); } } public Collection<Capital> autocomplete(FacesContext facesContext, UIComponent component, final String prefix) { Collection<Capital> persons = Collections2.filter(capitals, new Predicate<Capital>() { @Override public boolean apply(Capital capital) { if (prefix == null) { return true; } return capital.getState().toLowerCase().startsWith(prefix.toLowerCase()); } }); return persons; } public List<SelectItem> getCapitalsOptions() { return capitalsOptions; } public void setCapitalsOptions(List<SelectItem> capitalsOptions) { this.capitalsOptions = capitalsOptions; } public void setCapitals(List<Capital> capitals) { this.capitals = capitals; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }