Getting selected value (not index) & display value of select one choice programmatically in ADF

Sharing is Caring

Hello All,
This post is about a common requirement of getting selected value & display value of af:selectOneChoice in the managed bean
normally when we try to get selected value of  selectOneChoice in the managed bean (in ValueChange/ Validator or using binding) it returns the index of that row
so there is a little piece of code using that we can get selected value of choice component

    • I have used Departments table of HR Schema in the sample and created a lov on a transient field

 

 

    • Now dropped that ViewObject on the page as a table

 

 

    • Created a value change listener in managed bean fro DeptIdTrans but it returns the index of the selected value

 

 

    • Then i googled about it and used this method to solve my headache, try to get attributeValue instead of inputValue, see the code written in valueChaneListener in managed bean

    /**Value Change Listener of DeptIdTrans (to get selected and display value)
     * @param vce
     */
    public void deptIdVCE(ValueChangeEvent vce) {
        System.out.println("New Value is-" + vce.getNewValue());
        if (vce.getNewValue() != null) {
            this.setvalueToExpression("#{row.bindings.DeptIdTrans.inputValue}",
                                      vce.getNewValue()); //Updating Model Values
            Integer selectedCode =
                Integer.parseInt(this.getValueFrmExpression("#{row.bindings.DeptIdTrans.attributeValue}").toString());

System.out.println("******** Selected Value in List***** " + selectedCode);
System.out.println("*******Display Value in List ****" +
getValueFrmExpression("#{row.bindings.DeptIdTrans.selectedValue.attributeValues[1]}"));

        }
    }

 

    • There is two methods to set and get value in expression (EL)

    /**Method to set value in Expression (EL)
     * @param el
     * @param val
     */
    public void setvalueToExpression(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);
    }

    /**Method to get value from Expression (EL)
 * @param data
 * @return
 */
    public String getValueFrmExpression(String data) {
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = fc.getELContext();
ValueExpression valueExp = elFactory.createValueExpression(elContext, data, Object.class);
String Message = null;
Object obj = valueExp.getValue(elContext);
        if (obj != null) {
Message = obj.toString();
        }
        return Message;
    }

 

  • Again run the page and select any value in selectOneChoice (LOV) and see the result
Getting selected value

Cheers 🙂 Happy Learning

Related Posts

I’m an Oracle ACE, Blogger, Reviewer,
Technical Lead working on Oracle ADF

0 thoughts on “Getting selected value (not index) & display value of select one choice programmatically in ADF”

Leave a Reply

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