Monday, February 24, 2014

Using af:iterator in ADF

Sometimes  you need  iterative/dynamic behavior in the JSF page.like you need to display (0..n) no. of inputText fields depending upon the resultset.
<af:iterator> is a very good component for that purpose.

UseCase: You have a "text box" and a "add" button in a page.On click of "add" you want to add another text box in the page.
Implementation: 
1.Define a managed bean in the adfc-cofig.xml.






2. Write the Code in the managed bean.


    private List<String> itrText=new ArrayList<String>();
    public BlogMBean() {
    }
    public void addTextBox(ActionEvent actionEvent){
        itrText.add("Name "+itrText.size());
        AdfFacesContext.getCurrentInstance().addPartialTarget(actionEvent.getComponent().getParent());
    }
    public void setItrText(List<String> itrText) {
        this.itrText = itrText;
    }

    public List<String> getItrText() {
        return itrText;
    }


3. Wire the java code with the UI jspx page.

 <af:panelGroupLayout id="pgl1" layout="vertical">
                <af:commandButton text="Add" id="cb1"
                                  actionListener="#{viewScope.BlogMBean.addTextBox}"
                                  partialSubmit="true"/>
              <af:iterator value="#{viewScope.BlogMBean.itrText}" var="row">  
              <af:inputText label="Name" value="#{row}"/>
              </af:iterator>
   </af:panelGroupLayout>


4. Running example

No comments:

Post a Comment