Monday, December 7, 2009

af:selectOneChoice : get the combo box value for static and dynamic list

I have seen very common requirement to process the value selected in the Single Selection Combo Box at run time. Following example tells how to get this value at runtime in the backing bean for static list and dynamic list.

1. Static List
Static list allows to add a fixed list of user defined values. The following image shows the static list created on the Department Id.



Following steps show how to get the value from this list at runtime.
  • Define the valueChangeListener Property of the DepartmentId field to some method of a bean. e.g. SelectOneChoiceValueGetter:getSelectOneChoiceValue(ValueChangeEvent valueChangeEvent)
  • Now go to the above value change listener method and add the following code

public void getSelectOneChoiceValue(ValueChangeEvent valueChangeEvent) {
        // take the selectOneChoice object from the valueChangeEvenet
        CoreSelectOneChoice selectOneChoice = (CoreSelectOneChoice)valueChangeEvent.getSource();
        
        //get the LOV items list object
        List selectOnechoiceItemList = selectOneChoice.getChildren();

        //traverse the list and get all the children items
        for (int i = 0; i < selectOnechoiceItemList.size(); i++) {
            if (selectOnechoiceItemList.get(i) instanceof CoreSelectItem) {
                //get list item
                CoreSelectItem csi = (CoreSelectItem)selectOnechoiceItemList.get(i);
                
                // get the new value selected and type cast it into actual class
                oracle.jbo.domain.Number num = (Number)valueChangeEvent.getNewValue();

                //check if the list value is similar to the new selected value
                if ((((String)csi.getValue()).equals(num.toString()))) {
                    System.out.println("Item Label :" + csi.getLabel());
                    System.out.println("Item Value :" + csi.getValue());
                    // here is your value. store it or print it
                }
            }
        }
    }

2. Dynamic List
Dynamic list is created when  the number of values to be shown in the list box are not fixed. e.g. a new department can be added in an organisation in future and hence the newly added department is expected to appear in the list box at run time. In this case we create a dynamic list where the vales of the list box come at runtime from some master source. Following image shows the dynamic list created for the Department Id.





In the page definition file following code gets added.

<list id="EmployeesView1DepartmentId" IterBinding="EmployeesView1Iterator" StaticList="false" ListOperMode="0" ListIter="DepartmentsViewIterator">
<AttrNames><Item Value="DepartmentId"/></AttrNames>
<ListAttrNames>
<Item Value="DepartmentId"/>
</ListAttrNames>
<ListDisplayAttrNames>
<Item Value="DepartmentName"/>
</ListDisplayAttrNames>
</list>


Following steps show how to get the value of a dynamic list at run time.
  • As defined earlier for static list, define the valueChangeListener Property of the DepartmentId field to some method of a bean. e.g. DynamicListValueGetter:getDynamicListValue(ValueChangeEvent valueChangeEvent)
  • Now go to the above value change listener method and add the following code
Note : You should have the bindings set as the managed Property in the Managed Bean.


public void getDynamicListValue(ValueChangeEvent valueChangeEvent) {
// get the bindings to the current page components 
BindingContainer bindings = getBindings(); 
 
try {
//get the control binding to the id of the List as written in the Page Definition file
JUCtrlListBinding listBinding = (JUCtrlListBinding)bindings.get("EmployeesView1DepartmentId");


// as this is a value change listener method, so the list binding contains the OLD value of the combo box and not the NEW one. 
//The new value is available there in the valueChangeEvent as the index. So point the list binding to current selected index first
listBinding.setSelectedIndex(Integer.parseInt(valueChangeEvent.getNewValue().toString()));


// Now receive the selected row of the list binding
Row selectedValue = (Row) listBinding.getSelectedValue();


//from this row you can get the desired attributes
System.out.println( "Item Label :" + selectedValue.getAttribute("DepartmentName"));
System.out.println("Item Value :" + selectedValue.getAttribute("DepartmentId"));

catch (Exception ex)  
{ex.printStackTrace();}
}

When you run the page and select an item from the combo box, the label and value of selected item are displayed in the Jdeveloper Console. Once you have got the values you can do whatever you want.

    4 comments:

    1. Hi Neeraj,

      My requirement is that I have select one choice in af:table as a column. And on selection of some row i want to fetch the selected value of the dropdown but I am not able to do that.
      Please provide some help.

      Thanks in Advance.
      Kanika

      ReplyDelete
    2. hello Neeraj...
      I want that When I Choose a different value from the choice list then the records will be displayed according, for e.g if i select customer id one the the address related to customer id 1 will be displayed...so help me out with this..
      my code is....







      Thanks in advance.

      ReplyDelete
    3. Hi,
      I am using JDev 11.1.2.2.0. I try to implement this application. I am getting error in the following line
      " JUCtrlListBinding listBinding = (JUCtrlListBinding)bindings.get("EmployeesView1DepartmentId");

      "get" .
      What is wrong ?

      ReplyDelete
    4. Hi Infan,

      Below code works for me


      public static Object getSelectedValueFromSelectOneChoice(String selectedIndex,
      String iteratorName,
      String attributeName) {

      Object selectedValue = null;
      try {

      JUIteratorBinding listBinding =
      (JUIteratorBinding)ADFUtils.findIterator(iteratorName);
      listBinding.setCurrentRowIndexInRange(Integer.parseInt(selectedIndex));
      Row row = listBinding.getCurrentRow();

      selectedValue = row.getAttribute(attributeName);

      } catch (Exception ex) {
      ex.printStackTrace();
      }

      return selectedValue;
      }

      selectedValue = row.getAttribute(attributeName);

      ReplyDelete