Getting selected value from SelectOneChoice

The very common question across ADF Forums is "How to get the display value from the SelectOneChoice rather than index". In this article, we discuss various possible ways to answer this question.

Let's take an usecase where we need to display the selected value of the SelectOneChoice to the end user immediately after selecti
on.

SOC_SELECTED_VALUE_RT.JPG
 

Assume we have a Model-Driven List for Deptno attribute with the display value of Dname and selectOneChoice bound to Deptno attribute in jspx page

<af:selectOneChoice value="#{bindings.Deptno.inputValue}" label="Select Department"
                            required="true" shortDesc="#{bindings.Deptno.hints.tooltip}"
                            id="soc1" autoSubmit="true">
          <f:selectItems value="#{bindings.Deptno.items}" id="si1"/>
</af:selectOneChoice>
When we want the selected value, the common mistake we do is to use the same EL bound to the value property of SelectOneChoice component, but using this we get the index of the selected item instead rather than the value. This is because when we drag and drop attribute as SelectOneChoice on to the page, SelectItems gets generated with indexes as values.

Displaying selected value on to the jspx page
In this section, we see how to get selected value without writing single line of java code.
Create an outputText component with its value property bound to #{bindings.Deptno.attributeValue} instead of #{bindings.Deptno.inputValue} and make it refreshable based on list selection by adding partialTriggers property.
 
<af:outputText value = "Selected Value: #{bindings.Deptno.attributeValue}" id="ot1" partialTriggers="soc1"/>
The above code gives the Deptno value of the selected item. If the Deptno of 'SALES' is 30, 30 will get displayed on outputText on selecting 'SALES' from the list.

If we want 'SALES' itself to be displayed then the following EL should be used assuming Dname is the second attribute DeptView

<af:outputText value = "Display Value: #{bindings.Deptno.selectedValue ne ' ' ? bindings.Deptno.selectedValue.attributeValues[1] : ''}" id="ot2" partialTriggers="soc1"/>

Inside value change listener
Evaluating above EL expressions inside ValueChangeListener doesn't give the current selected value instead gives the previously selected value as the selected value doesn't get updated to the model by the time ValueChangeListener gets invoked.

In this case, before accessing the selected value, we need to update the model first.

Here is the sample code:
public void valueChanged(ValueChangeEvent valueChangeEvent) {
    this.setValueToEL("#{bindings.Deptno.inputValue}", valueChangeEvent.getNewValue()); //Updates the model
    System.out.println("\n******** Selected Value: "+resolveExpression("#{bindings.Deptno.attributeValue}"));
    System.out.println("\n******** Display Value: "+resolveExpression("#{bindings.Deptno.selectedValue ne ' ' ? bindings.Deptno.selectedValue.attributeValues[1] : ''}"));
}
 
public Object resolveExpression(String el) {     
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ELContext elContext = facesContext.getELContext();
    ExpressionFactory expressionFactory =  facesContext.getApplication().getExpressionFactory();       
    ValueExpression valueExp = expressionFactory.createValueExpression(elContext,el,Object.class);
    return valueExp.getValue(elContext);
}

public void setValueToEL(String el, Object val) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ELContext elContext = facesContext.getELContext();
    ExpressionFactory expressionFactory =   facesContext.getApplication().getExpressionFactory();
    ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class);
    exp.setValue(elContext, val);
}   
Note: If user wants location value for the selected department, index 2 should be used in the EL instead of 1 assuming DeptView has its attributes in the order of Deptno, Dname and Loc.
Comments:

Hi,

i tried this #{bindings.Deptno.selectedValue.attributeValues[1]}, but i got error because "attributeValues" method no exists.

advice please.

jdev 11.1.1.2.0

Posted by guest on March 08, 2012 at 09:44 AM IST #

Hi,

Can you please make sure if Deptno is a list binding or not in your pagedef?

Thanks,
Sireesha

Posted by Sireesha on March 08, 2012 at 01:06 PM IST #

helpful

Posted by guest on May 16, 2012 at 11:09 AM IST #

It really helped. Can you please tell me how to use the SelectManychoice component bindings.

Posted by Levelfive Solutions on January 29, 2013 at 09:24 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Tips and Tricks from Oracle's JDeveloper & ADF QA

Search

Archives
« May 2015
SunMonTueWedThuFriSat
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      
Today