Wednesday, July 25, 2012

How to progrmatically List of Values in SelectOneChoice

This blog explains about how to programatically populate list of values dynamically in a <af:SelectOneChoice> component


Step1 : Drag <af:selectOneChoice> component from component pallet to page

Step2 : JSFF - Bind <f:selectItems> to a backing bean method as shown below.

 <af:selectOneChoice value="#{bindings.CustomerID.inputValue}"
             label=""
             required="#{bindings.CustomerID.hints.mandatory}"
             shortDesc="#{bindings.CustomerID.hints.tooltip}"
             id="cusID" autoSubmit="true"
             valuePassThru="true">
       <f:selectItems 
                value="#{pageFlowScope.EquipmentPoolBean.customerList}"
                id="si2"/>
       </af:selectOneChoice>
           
 Step2:  Implement corresponding backing bean method to return List<SelectItem> as shown below
   

       

   public List getCustomerList()
  {
// Prepare list of values based on your requirement
    List customerList = new ArrayList();
    customerList.add(new SelectItem("CUST1","Customer1");
    customerList.add(new SelectItem("CUST2","Customer2");
    customerList.add(new SelectItem("CUST3","Customer3");
    return customerList;
  }
       
 




1 comment:

  1. hi, i was trying to execute above code but when i select value in drop-down (e.g. Customer1), the value binding to 'CustomerID' is not 'CUST1' but '0'. i.e. index value. how to solve that problem?

    ReplyDelete