Pre-populate selected values in viewObject based shuttle component (multiple selection)- Oracle ADF

Sharing is Caring

Hello All,
This post talks about a common development requirement
while using shuttle component sometimes we need to show some values on the selected side of af:selectManyShuttle , so to do this normally we use a custom shuttle component that is populated from managed bean code.
but when I have all data in a view object so why should I iterate viewObject to get all data in the managed bean and then set some values in an ArrayList to populate selected shuttle values.
it is time-consuming and requires a lot of coding, so for the viewObject based shuttle, we can do it in an efficient way.

    • In this example I’m using Departments table of HR schema to populate af:selectManyShuttle, I think you all know how to create a shuttle in ADF
    • So next is to populate selected shuttle values on some event, created a fusion web application using Departments table

 

    • It’s time to drop Departments ViewObject on the page as shuttle component

<af:selectManyShuttle value="#{bindings.Departments1.inputValue}" label="#{bindings.Departments1.label}"
                                      id="sms1" size="15" binding="#{pageFlowScope.ShuttleSelectBean.shuttleBind}"
                                      partialTriggers="b1">
                    <f:selectItems value="#{bindings.Departments1.items}" id="si1"/>
                    <f:validator binding="#{bindings.Departments1.validator}"/>
                </af:selectManyShuttle>

 

    • Suppose on a button click I want to set selected values in the shuttle, so I have created an actionEvent for the button in the managed bean and component binding of af:selectManyShuttle, so basic structure of the page is like this

 

    • See the code of actionEvent , create an arrayList and add all values in it that you want to set as selected values and then just set the value in shuttle using binding

    /**Method to set selected values in af:selectManyShuttle
     * @param actionEvent
     */
    public void selectValuesShuttle(ActionEvent actionEvent) {
        DCIteratorBinding iter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator");
        ViewObject vo = iter.getViewObject();
        RowSetIterator rsi = vo.createRowSetIterator(null);
        ArrayList listVal = new ArrayList(20);

        while (rsi.hasNext()) {
Row nextRow = rsi.next();
            if (nextRow.getAttribute("DepartmentId") != null) {
listVal.add(nextRow.getAttribute("DepartmentId"));
            }
        }
shuttleBind.resetValue();// Reset Shuttle Component
System.out.println("Setting values -" + listVal);
shuttleBind.setValue(listVal.toArray()); // Setting selected values 
AdfFacesContext.getCurrentInstance().addPartialTarget(shuttleBind);
    }

 

    • Now run this page and test your shuttle component

 

 

shuttle component

Cheers- Happy Coding

    • But this method works only when you have to set values on some action or other event after page load, because before page load , binding of shuttle component will not be accessible
    • So if you have a requirement of setting selected values on page load then you must use a list defined in the managed bean and bind it to  value property of shuttle component, value property denotes selected values of the shuttle

 

 

    • And just write same code on getter of List, in this case selected values in shuttle are populated on page load

    List pageLoad = new ArrayList(20);
    public void setPageLoad(List pageLoad) {
        this.pageLoad = pageLoad;
    }

    public List getPageLoad() {
DCIteratorBinding iter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator");
ViewObject vo = iter.getViewObject();
RowSetIterator rsi = vo.createRowSetIterator(null);
        while (rsi.hasNext()) {
Row nextRow = rsi.next();
            if (nextRow.getAttribute("DepartmentId") != null) {
pageLoad.add(nextRow.getAttribute("DepartmentId"));
            }
        }
        return pageLoad;
    }

You can use both methods as per your requirement 🙂

Sample ADF Application- Download

Related Posts

Leave a Reply

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