sachin11
sachin11

Reputation: 1167

Get the selected value from selectOneChoice in ADF and not the index

I am new to JDeveloper and ADF and I am facing a bit of a problem in getting the selected value from selectOneChoice component. This is the valuChangeListener:

public void versionValueChangeListener(ValueChangeEvent valueChangeEvent) {
    System.out.println(valueChangeEvent.getOldValue().toString());
    System.out.println(valueChangeEvent.getNewValue().toString());

}

This is giving the index of the selected choice and not the text itself. How can I get the text and not the index? This is the code for the selectOneChoice:

<af:selectOneChoice value="#{bindings.Version.inputValue}"
                                      label="#{bindings.Version.label}"
                                      required="#{bindings.Version.hints.mandatory}"
                                      shortDesc="#{bindings.Version.hints.tooltip}"
                                      id="soc3" autoSubmit="true"
                                      valueChangeListener="#{savesBean.versionValueChangeListener}">
                    <f:selectItems value="#{bindings.Version.items}" id="si3"/>
                  </af:selectOneChoice>

Thanx :)

Upvotes: 1

Views: 29117

Answers (4)

Oded Deutch
Oded Deutch

Reputation: 33

This worked for me

BindingContainer binding = BindingContext.getCurrent().getCurrentBindingsEntry();
JUCtrlListBinding fc =(JUCtrlListBinding) binding.get(nameOfTheDataControl);
String selectedValue = (fc.getListIterBinding().getRowAtRangeIndex(newIndexSelected).getAttribute(nameOftheColu 
mnInTheDataControl)).toString();

Upvotes: 0

XpiritO
XpiritO

Reputation: 2827

You can find out the solution for this on the following URL: https://blogs.oracle.com/adf/entry/getting_selected_value_from_selectonechoice

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);
}

Upvotes: -1

GrrLex
GrrLex

Reputation: 9

There is a property for the select one choice item on the jsp/jsf page that allows you to pass the actual object value or to pass the index value from the list. Click on the select one choice item in the jsp/jsf page, then hit the bindings tab at the bottom, go to the page definition (you will see it at the top of the bindings page) and the select one choice item will be highlighted in the page definition file that is now open. If you look at the property inspector from here - there is a property called "SelectItemValueMode" by default it is set to the ListIndex value, you can change it to the ListObject from here. It is the last property listed for the select one choice list object in the properties window from the page definition file.

Upvotes: 0

Daniel
Daniel

Reputation: 37061

This is how the guys at Orcle do it

How-to get the selected af:selectOneChoice Label although in my opinion it can be done in other way...

I think you better build a map in which the index will be the key and the value is the label

than in versionValueChangeListener you'll access the map something like this :

myMap.get(valueChangeEvent.getNewValue().toString());

Upvotes: 1

Related Questions