Sunday, November 8, 2009

Getting the Value from a SelectOneChoice List (Not Index)

One of the Issue I found when I use af:selectOneChoice component that when I want to get the selected value from one of the selectOneChoice attribute from backingbean I found the return value is the index of selected value not the value. (Using HR Schema)
assume we have an EmployeesView which has a DepartmentId (as a lookup to DepartmentView) and want to get the the value of selected Department. To solve this problem do this :

BindingContainer bindings =BindingContext.getCurrent().getCurrentBindingsEntry();
JUCtrlListBinding listBinding =(JUCtrlListBinding)bindings.get("DepartmentId");
Row selectedValue = (Row) listBinding.getSelectedValue();
System.out.println(selectedValue.getAttribute("DepartmentId"));

Where:

- DepartmentId the selectOneChoice Attribute
which you want to get its value.
- selectedValue is the row of the lookup attribute (In this case will be the row of the selected department).

11 comments:

  1. HI...Samesh thanks for this useful information.
    but i want to know..u provide Id where from this comes?
    System.out.println(selectedValue.getAttribute("Id"));

    ReplyDelete
  2. Hi Charu,

    selectedValue is a row of department you can get from it what you want as Id , DepartmentName , ..

    System.out.println(selectedValue.getAttribute("Id"));
    System.out.println(selectedValue.getAttribute("DepartmentName"));

    ReplyDelete
  3. Hi,
    If I use your solution inside valueChangeListener I obtain OLD value of my selectOneChoice, which is not I expect :(

    ReplyDelete
  4. Hi penser,
    If you want to use valueChangeListener you should add this line before you get the selectedValue :
    listBinding.setSelectedIndex(Integer.parseInt(valueChangeEvent.getNewValue().toString()));

    Your code will be :
    BindingContainer bindings =BindingContext.getCurrent().getCurrentBindingsEntry();
    JUCtrlListBinding listBinding =(JUCtrlListBinding)bindings.get("DepartmentId");
    listBinding.setSelectedIndex(Integer.parseInt(valueChangeEvent.getNewValue().toString()));
    Row selectedValue = (Row) listBinding.getSelectedValue();
    System.out.println("Id== "+selectedValue.getAttribute("DepartmentId"));
    System.out.println("Name== "+selectedValue.getAttribute("DepartmentName"));

    ReplyDelete
  5. Hi Samesh,

    I am using ur code and try to get the selected value. But it always returns the very first value of my selectOneChoice list, cant able to get the selected value.

    When I print the listBinding value it also always print 0!!!!!. Can you please let me know what is that deptmentID which ur using. I am using the Decode which i drag and drop from my Application ModuleDC

    ReplyDelete
  6. I believe binding.get("Decode"); is wrong in my case. Since I drag and drop the Decode from my Application Module DC using that one to get. B ut seems that is not in the list. How can I see all the list in the binding.

    Or how can get the correct attribute name of my selectOneChoice from my page?

    ReplyDelete
  7. The solution you provided for valueChangeListener works! Thanks!

    ReplyDelete
  8. شكرا على المعلومات القيمة

    ReplyDelete
  9. WOW! your solution works perfect! Thanks so much for help us

    ReplyDelete
  10. Your solution is working thanks...

    ReplyDelete