Monday 28 December 2015

ADF | Fetching Multiple selected Rows from a Table populated from POJO in ADF

Hi,

This is again an Extension to my previous post on how we can perform edit on a table populated from POJO. http://adfjavacodes.blogspot.com/2015/12/adf-performing-edit-in-table-from-pojo.html

Everything is same from my previous post.
There are certain changes in the application. They are :

Here I have enabled Multiple row selection for the table from the property inspectior to
 <af:table var="row" rowBandingInterval="0" id="t1" value="#{viewScope.BookBean.bookInfo}"
                                  rowSelection="multiple" binding="#{viewScope.BookBean.tableBinding}">

I have also created a binding of the table for getting the  Multiple selected rows from the table.
Here is the code of the of the Bean where I have created a method that will fetch the selected rows from the table.


package edittableonpojo.view.bean;

import edittableonpojo.view.dc.BookInfo;

import java.util.ArrayList;

import java.util.Iterator;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.view.rich.component.rich.data.RichTable;

import org.apache.myfaces.trinidad.model.RowKeySet;

public class BookBean {
    private ArrayList<BookInfo> bookDtls = new ArrayList<BookInfo>();
    private RichTable tableBinding;

    public BookBean() {
        bookDtls.add(new BookInfo("1A", "The Alchemist", "Paulo Cohelo"));
        bookDtls.add(new BookInfo("2A", "Game Of Thrones", "George R. R. Martin"));
        bookDtls.add(new BookInfo("3A", "Five Point Someone", "Chetan Bhagat"));
        bookDtls.add(new BookInfo("4A", "Harry Potter", "J.K.Rowling"));
        bookDtls.add(new BookInfo("5A", "Wings of Fire", "A.P.J Abdul Kalam"));
    }

    public ArrayList<BookInfo> getBookInfo() {
        return bookDtls;
    }
    
    public void printValues(ActionEvent ace){
        for(BookInfo row : bookDtls){
            System.out.println("Row : "+row.toString());
        }
    }

    public void fetchSelectedValuesAL(ActionEvent actionEvent) {
        ArrayList<BookInfo> selectedRows = new ArrayList<BookInfo>();
        RowKeySet keySet = getTableBinding().getSelectedRowKeys();
        Iterator<Object> itr = keySet.iterator();
        while(itr.hasNext()){
            // next gives the index of the selected row
            Object next = itr.next();
            // here we have fetched the row on index from the bookDtls list
            selectedRows.add(bookDtls.get((Integer)next));
        }
        System.out.println("Selected Rows are : "+selectedRows.toString());
    }

    public void setTableBinding(RichTable tableBinding) {
        this.tableBinding = tableBinding;
    }

    public RichTable getTableBinding() {
        return tableBinding;
    }
}

On running the application ,  I have selected Two rows from the table.

On clicking "Get Selected Values" button.



Here is the Sample Application : EditableTableOnPOJO.rar

No comments:

Post a Comment