Sunday, October 23, 2016

Customize Databound Fixed List of Values in Oracle ADF


Hi folks,

Oracle ADF is a one of the most advanced framework use by many of companies nowadays. It will allow developers to build an MVC architecture based applications, basically ADF make developer life essayer than any other framework existing in today.

This post for developers who has basic knowledge with ADF.

Creating a fixed list dropdown, usually used for some static values. Let’s assume that we have a requirement of the selecting two type of age ranges for some app. 45> allowed and 45< not allowed, in DB side these operation handle by the Boolean type variable. 45> true and 45< false.

When we are checking the documentations of Oracle ADF, the recommended way is using a databound fixedlist,


But it will be a mess, when we need to customize the display value and DB item value, because we need to mention the exact same list which include in DB side, If we consider the scenario like above mentioned, we can’t use different value and label like this,

1:  <af:selectOneChoice value="#{bindings.Userstatus.inputValue}"  
2:            label="#{bindings.Userstatus.label}: ">  
3:   <f:selectItems value="#{bindings.Userstatus.items}" itemLabel="#{bindings.Userstatus.items == true ? Above 45 : Bellow 45}"/>  
4:  </af:selectOneChoice>  

Because it doesn’t allow to customize with different itemLabel and value, this drop down always display true and false instead of “Above 45” and “Bellow 45”.

In order to solve that, we can create a programmatic fixed list, this approach allows you to customize value and label according to our requirement.

According to SelectItem documentation, six (6) override constructors allow us to define following parameters,


  •          java.lang.Object value
  •          java.lang.String label
  •          java.lang.String description
  •          boolean disabled
  •          boolean escape

We can define fixed list in bean class and, simply customize dropdown value list.

1:  List<SelectItem> userStatus;  
2:    public void setUserStatus(List<SelectItem> userStatus) {  
3:      this.userStatus = userStatus;  
4:    }  
5:    public List<SelectItem> getUserStatus() {  
6:            if (userStatus == null) {  
7:        userStatus = new ArrayList<SelectItem>();  
8:        userStatus.add(new SelectItem(true,"Above 45"));  
9:        userStatus.add(new SelectItem(false,"Bellow 45"));  
10:            }  
11:      return userStatus;  
12:    }  
MyBean.java

1:  <af:selectOneChoice value="#{bindings.Userstatus.inputValue}"  
2:            label="#{bindings.Userstatus.label}: ">  
3:   <f:selectItems value="#{MyBean.userStatus}" />  
4:  </af:selectOneChoice>  

Happy Coding,

Regards,
Denuwan Himanga


No comments:

Post a Comment