Please disable your adblock and script blockers to view this page

Search this blog

Friday 9 August 2013

Programmatically populate values in a af:selectOneChoice component in ADF

In this tutorial you will see that how to populate selectOneChoice list from managed bean, sometimes we need to use custom list that are not model driven, this scenario can be implemented there

follow steps -

  • First of all create a fusion web application and a page in it
  • Now create a variable of type java.util.List in managed bean, and generate its accessors

  •     List<SelectItem> customList;
    
        public void setCustomList(List<SelectItem> customList) {
            this.customList = customList;
        }
    
        public List<SelectItem> getCustomList() {
            return customList;
        }
    

  • this list contains value of type javax.faces.model.SelectItem; that is supported by af:selectOneChoice
  • now time to add values in list, so to add values in this list i have written this code in getter of variable

  •     public List<SelectItem> getCustomList() {
            if (customList == null) {
                customList = new ArrayList<SelectItem>();
                customList.add(new SelectItem("i1","Item 1"));
                customList.add(new SelectItem("i2","Item 2"));
                customList.add(new SelectItem("i3","Item 3"));
                customList.add(new SelectItem("i3","Item 4"));
                customList.add(new SelectItem("i5","Item 5"));
        }
            return customList;
        }
    

  • Managed bean part is done, now add this to view layer



  • Drop choice list in page from component palette
  •  Now bind choice list to bean List


  • See the page source after whole setup-

  • <?xml version='1.0' encoding='UTF-8'?>
    <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
              xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
        <jsp:directive.page contentType="text/html;charset=UTF-8"/>
        <f:view>
            <af:document title="listPage.jspx" id="d1">
                <af:form id="f1">
                    <af:panelGroupLayout id="pgl1" layout="vertical" halign="center">
                        <af:spacer width="10" height="10" id="s1"/>
                        <af:selectOneChoice label="Custom List" id="soc1" contentStyle="font-weight:bold;color:darkgreen;">
                            <f:selectItems value="#{ListPolulatebean.customList}" id="si1"/>
                        </af:selectOneChoice>
                    </af:panelGroupLayout>
                </af:form>
            </af:document>
        </f:view>
    </jsp:root>
    

  • Run this page and see programmatic choice list-


11 comments :

  1. Is this list in a session managed bean? Cause otherwise the items will be lost in every request.

    ReplyDelete
    Replies
    1. Luis

      Bean scope is greater than request, you can use view or pageFlow as per your requirement

      Ashish

      Delete
  2. how to set default value for it

    ReplyDelete
  3. HI Awasthi
    as per this scenario i am getting "i1" in side the valuchengeevent.getNewValue()
    but if i need "Item 1" inside valuchengeevent.getNewValue(),
    how will i get. please help me.

    ReplyDelete
  4. HI Awasthi

    How i can populate programmatically please help me :)

    ReplyDelete
  5. I want to remove the blank item form the dropdown. How I can do that?

    ReplyDelete
    Replies
    1. You can set unselected label property of selectOneChoice

      Delete