Tuesday 28 May 2013

How to get all the selected rows from a table in ADF.

How get all the selected rows from the UI ADF table.

For example you want to print all the departments which you have selected in the UI..
 
1. In the Table property inspector make rowSelection="multiple". And
Go to the Binding in Advanced option. Select the managed bean (or create new managed Bean) where you want to access this this Table. And create new property. (just give one new name)



Like below it will generate the code in the managed bean for the RichTable.

   private RichTable departmentTable;

   public void setDepartmentTable(RichTable departmentTable) {
        this.departmentTable = departmentTable;
    }
    public RichTable getDepartmentTable() {
        return departmentTable;
    }

2. Now you can access the selected rows from the table using below code.

RowKeySet selectedRowKeySet = this.getDepartmentTable().getSelectedRowKeys();
System.out.println("Number of rows:"+ selectedRowKeySet.size());

Using "selectedRowKeySet" you can iterate and get all the department data by rowwise

You can write this code in the SelctionListner of the Table itself so that if you want to perform any operations on selction of rows. as shown below.

FacesContext fc =FacesContext.getCurrentInstance();
RowKeySet selectedRowKeySet = this.getDepartmentTable().getSelectedRowKeys();
       
ExpressionFactory ef = fc.getApplication().getExpressionFactory();
 MethodExpression me = ef.createMethodExpression(fc.getELContext(),       "#{bindings.DepartmentVO1.collectionModel.makeCurrent}", null,
                                                     new Class[] { SelectionEvent.class });
        me.invoke(fc.getELContext(), new Object[] { selectionEvent });
        System.out.println("Number of rows:"+ selectedRowKeySet.size());

This works properly even if you have the dependent fragments. Like if you have Employee fragment depend on Departments selection.


The image is shown below where you have selected multiple rows.




No comments:

Post a Comment