Working with af:iterator and af:forEach programmatically (Populate values using POJO from Managed Bean)

Sharing is Caring

This is another post about Working programmatically with ADF (populating af:iterator and af:forEach programmatically )

Previously I have posted about populating af:iterator and af:forEach using ADF BC and binding layer to show the master-detail relation
Implementing master/detail tree relation using ADF Iterator and af:forEach for better UI designs – Oracle ADF

For this post, I am populating employee name and it’s department name using List data structure, to get and set the value of attributes

Created a java bean class, it has 2 variable for both attributes

public class EmployeeDet {
    public EmployeeDet(String name, String deptName) {
        this.name = name;
        this.deptName = deptName;
    }
    //Attribute to display EmployeeName and Department Name
    private String name;
    private String deptName;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setDeptName(String deptName) {
this.deptName = deptName;
}

public String getDeptName() {
return deptName;
}
}

Next step is to create a managed bean to populate data in af:iterator and af:forEach , this managed bean makes use of

EmployeeDet

java bean class to add data in the same format for all items of iterator and forEach. A List data structure is used to pass all values to the iterator. See code of managed bean

//Constructor-populate default records
public PopulateIteratorBean() {

EmployeeDet obj = new EmployeeDet("Ashish Awasthi", "Oracle ADF");
employeeDetail.add(obj);
obj = new EmployeeDet("Alex Smith", "Java");
employeeDetail.add(obj);
obj = new EmployeeDet("James S", "PHP");
employeeDetail.add(obj);
}
//ArrayList to poplate data in af:iterator and af:forEach
private List<EmployeeDet> employeeDetail = new ArrayList();

public void setEmployeeDetail(List<EmployeeDet> employeeDetail) {
this.employeeDetail = employeeDetail;
}

public List<EmployeeDet> getEmployeeDetail() {

return employeeDetail;
}

Now drop af:iterator on the page and set its properties like value, var etc

using var reference of the iterator, set value in output text to show Employee Name and DepartmentName, see XML source of af:iterator

<af:panelGroupLayout id="pgl1" layout="horizontal">
                        <af:iterator id="i1" value="#{viewScope.PopulateIteratorBean.employeeDetail}" var="item">
                            <af:panelBox id="pb2" showDisclosure="false">
                                <f:facet name="toolbar"/>
                                <af:panelGroupLayout id="pgl3" layout="horizontal">
                                    <af:outputText value="#{item.name}" id="ot1"
                                                   inlineStyle="font-weight:bold; font-size:medium; color:#0572ce;;"/>
                                    <af:spacer width="2" height="0" id="s1"/>
                                    <af:outputText value="(#{item.deptName})" id="ot2"
                                                   inlineStyle="font-weight:bold;font-size:small;color:red;"/>
                                </af:panelGroupLayout>
                            </af:panelBox>
                        </af:iterator>
                    </af:panelGroupLayout>

on running it looks like this-

af:iterator

 

Now I am doing the same for af:forEach

<af:panelGroupLayout id="pgl2" layout="horizontal">
                        <af:forEach items="#{viewScope.PopulateIteratorBean.employeeDetail}" var="feach">
                            <af:showDetailHeader text="#{feach.deptName}" disclosed="true" id="sdh1">
                                <af:outputText value="#{feach.name}" id="ot3"
                                               inlineStyle="font-weight:bold; font-size:medium; color:#0572ce;;"/>
                            </af:showDetailHeader>
                        </af:forEach>
                    </af:panelGroupLayout>

on running it looks like this, Isn’t it look good 😉

Sample ADF Application-Download
Thanks, Happy Learning 🙂

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

0 thoughts on “Working with af:iterator and af:forEach programmatically (Populate values using POJO from Managed Bean)”

  1. Hi,
    nice post, and nice blog. I have a question, what if i want to let data flow on second line, then third line….etc, like the view in merchandising websites. I am stuck in this, so your answer will help me a lot.
    Thanks.

  2. Hi,
    Thanks very much for publishing this! Would you know if and can not be used within ? I've tried both using ADF 11.1.1.7.1, and neither seems to work.

    I appreciate any help you offer.
    Will

Leave a Reply

Your email address will not be published. Required fields are marked *