Tuesday, April 2, 2013

Get all the selected rows ADF table in backing bean

Requirement: We have a table with multiple row selection enabled and we have to get all the selected rows in the backing bean

Note: In this example we are using user portfolio in stock management application

Steps:

1. Add an accessor in the backing bean and bind it to the table as below



2. Drag and drop adf button in the jspx page with the table <af:table> as below.



3. Bind a backing bean method to the above button created as below



4. Write the following code in the backing bean and you have to import the following packages

import java.util.Iterator;
import java.util.List;
import javax.faces.event.ActionEvent;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.jbo.Key;
import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;
import org.apache.myfaces.trinidad.model.RowKeySet;

    public void getSelectedRows(ActionEvent actionEvent) {
        // Add event code here...
        RowKeySet selectedEmps = getPfTable().getSelectedRowKeys();   
        Iterator selectedEmpIter = selectedEmps.iterator();
        DCBindingContainer bindings =
                          (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding empIter = bindings.findIteratorBinding("PortfolioStocks2Iterator");
        RowSetIterator empRSIter = empIter.getRowSetIterator();
         while(selectedEmpIter.hasNext()){
           Key key = (Key)((List)selectedEmpIter.next()).get(0);
           Row currentRow = empRSIter.getRow(key);
           System.out.println(currentRow.getAttribute("Stockid"));
         }
         //return null;
    }

 5. Get the iterator to be bound from the page definition as below

1 comment: