Thursday, March 9, 2017

Get Selected Value and Id from selectOneChoice in ADF

I must confess that every time i come across this requirement i have to google for it.
I go across 10 different links before arriving at solution.

So i am writing this post for MY FUTURE REFERENCE, so i that i can go through the post and get an answer for me!!!

Problem:

Suppose i have a LOV populated as



Lets say the value/Id combination in the lov is:
10/A
20/B
30/C


I need to find a way to get both Id and value of the selected item.


Solution:

Lets assume the af:selectOneChoice  is populated as :



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


The following will print the Value on the jspx page as:

<af:outputText value = "Id : #{bindings.TypeNameList.attributeValue}" id="ottext2" partialTriggers="soc3"/>


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


On Backing bean if you want to fetch them, follow this:



    /**Method to get value from Expression (EL)
     * @param data
     * @return
     */
    private 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;
    }


and call it as :
getValueFrmExpression("#{bindings.TypeNameList.selectedValue}")



This might be a simple problem but i come across this all the time and struggle every time to get the value