Tuesday, May 13, 2014

How to access the selected value in af:selectonechoice

Common problem with af:selectonechoice component is when trying to get the selected value it returns the index of the selected item.

I found a turn around for it. It can be done in multiple ways, one of which is explained here.

1. Make sure the attribute which is expected to be selected is defined in the page def as. For example, on the select of department i would like to fetch the location of the selected department:



2. Add the following code in vcl:

    public void deptVCL(ValueChangeEvent valueChangeEvent) {
        BindingContext bctx = BindingContext.getCurrent();
        BindingContainer bindings = bctx.getCurrentBindingsEntry();
        AttributeBinding applicationNameBinding =   (AttributeBinding)bindings.getControlBinding("LocationId");
        String locatriondId= applicationNameBinding.getInputValue().toString();
        System.out.println("Old Location name   "+locatriondId);
        FacesContext contxt = FacesContext.getCurrentInstance();
        valueChangeEvent.getComponent().processUpdates(contxt);
        locatriondId = applicationNameBinding.getInputValue().toString();
        System.out.println("New Location name     " + locatriondId);
    }

3. The output in console is as follows:






No comments:

Post a Comment