Getting all selected rows in ADF Table with multiple rows selection enabled

When we build a web application which contains an ADF Table (with multiselect option), in many cases, we require to get all the selected rows to process through backing bean. This example will illustrate how to achieve that.

Assuming that we already have an application, that contains an ADF Table with multi-selection enabled (i.e the web page look something like the image shown below).

empTableRT-thumb-301x302-20887.jpg

img empTableRT : Table based on Emp

To access the table in the backing bean (through command button click, for ex.), we add an attribute with accessors in the backing bean and bind it to ADF Table

    RichTable empTable;
    public void setEmpTable(RichTable empTable) {
        this.empTable = empTable;
    }


    public RichTable getEmpTable() {
        return empTable;
    }

code snippet of the af:table in jspx page.

<af:table value="#{bindings.EmpView1.collectionModel}" var="row"
                  rows="#{bindings.EmpView1.rangeSize}"
                  emptyText="#{bindings.EmpView1.viewable ? 'No data to display.' : 'Access Denied.'}"
                  fetchSize="#{bindings.EmpView1.rangeSize}"
                  rowBandingInterval="0"
                  selectedRowKeys="#{bindings.EmpView1.collectionModel.selectedRow}"
                  selectionListener="#{bindings.EmpView1.collectionModel.makeCurrent}"
                  rowSelection="multiple" id="t1"
                  binding="#{backingBeanScope.EmpBean.empTable}">

Let us add a command button in the jspx page to print down the "Ename"s of selected rows.

<af:commandButton text="Print selected Emps" id="cb1" 
      action="#{backingBeanScope.EmpBean.printSelectedEmpNames}"/>

The method printSelectedEmpNames in the backing bean gets the selected row keys from the table, gets the corresponding Enames from the iterator (on which the table is based on) and prints them.
public String printSelectedEmpNames() {
        RowKeySet selectedEmps = getEmpTable().getSelectedRowKeys();    
        Iterator selectedEmpIter = selectedEmps.iterator();
        DCBindingContainer bindings =
                          (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding empIter = bindings.findIteratorBinding("EmpView1Iterator");
        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("Ename"));
         }
         return null;

      }

We run the page and click button to check the selected rows.

empTableMultiSelect-thumb-321x325-20890.jpg

As we've selected 7 rows, clicking on the "Print selected Emps" button is expected to print 7 Enames.

But..... It just prints only one Ename.

CLARK

Well, this is because of the selectedRowKeys property of the af:table. Let us try removing that and see if we get the expected output.

<af:table value="#{bindings.EmpView1.collectionModel}" var="row"
                  rows="#{bindings.EmpView1.rangeSize}"
                  emptyText="#{bindings.EmpView1.viewable ? 'No data to display.' : 'Access Denied.'}"
                  fetchSize="#{bindings.EmpView1.rangeSize}"
                  rowBandingInterval="0"

                  selectionListener="#{bindings.EmpView1.collectionModel.makeCurrent}"
                  rowSelection="multiple" id="t1"
                  binding="#{backingBeanScope.EmpBean.empTable}">

Now, when we run the page, select 7 rows and click on the print button, it prints out

JONES
ALLEN
CLARK
WARD
SMITH
MARTIN
BLAKE

That is what we expected. Now, the question is why this happens?. It is because, since value of this selectedRowKeys property is #{bindings.EmpView1.collectionModel.selectedRow}, the selectedRowKeys will contain only the "row which is selected last". By unsetting this attribute, we let the table to push all the selected rows to selectedRowKeys, which will help us during

getEmpTable().getSelectedRowKeys();

in the backing bean. JDeveloper automatically adds this property to the ADF Table when it is created by dragging from the data control. So, removing this property from the af:table would get us desired result.


Comments:

Hi all ,
In my project i need to select multiple rows from a table.

The code i have tried using after looking forum are as below:-

==========
1. RowKeySet selectRegion=getReAssignUserToLocationTable().getSelectedRowKeys();
Iterator itr=selectRegion.iterator();
DCBindingContainer binding=(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding reg=binding.findIteratorBinding("accountContactsIterator");
RowSetIterator rtr=reg.getRowSetIterator();
System.out.println("setting value"+rtr);
while(itr.hasNext()){
System.out.println("inside loop");
Key key=(Key)((List)itr.next()).get(0);
System.out.println("inside loop2"+key); // till this point its working good
Row currentRow = rtr.getRow(key); // fetching null at this stage
System.out.println("inside loop3"+currentRow);
===> System.out.println(currentRow.getAttribute("LNAccountSeqId")); // due to above line fetching null we getting "null pointer exception"

}
=======================================
In the above method i am getting "null pointer exception" at marked step( ==>), after debugging i came to know that the iterator from where i am trying to fetch row data is returning null while quering with row key inside the loop. "we are not using VIEW OBJECT in our project because of which above code is not working for us. we are using "non ADF iterator and jaxp java classes" to call the webservice.
in the above code we are able to populate rowkeys set and able to print the keys value for the rows we selecting from table.

==> need your help in order to fetch data of a selected row inside while loop for a key. kindly assist.

-==================================
And another option we tried is below, In this we are using only table to get keys and fetching data from the row but this also getting null pointer exception at marked row (====>),
i am not understanding why for this method also i am getting same error ???
=====================================
:-
2. RowKeySet rowKeys = reAssignUserToLocationTable.getSelectedRowKeys();
CollectionModel cm =(CollectionModel)reAssignUserToLocationTable.getValue();
System.out.println(rowKeys);
Object oldRowKey = reAssignUserToLocationTable.getRowKey();
try{
for (Object rowkey:rowKeys) {
System.out.println("inside for loop");
int i =rowkey.hashCode();
System.out.println(" i = " +i);
System.out.println("row data is "+rowkey);

reAssignUserToLocationTable.setRowKey(rowkey);
System.out.println("index="+i);
System.out.println("row"+ reAssignUserToLocationTable.getRowCount());// working fine till this point
Row r= (Row)reAssignUserToLocationTable.getrow; // fetching null
=====> System.out.println("row fetched"+ r.getAttribute("LNAccountSeqId")); // due to above line fetching null we getting " null pointer exception'"

}
}
finally{
//Restore the original rowKey
reAssignUserToLocationTable.setRowKey(oldRowKey);
}
==========================================
waiting for valuable suggestion !
thanks
mohit

Posted by guest on June 30, 2011 at 04:43 AM IST #

Can you post the version for ADF 10. <af:table> in 10g misses some attributes as compared to 11g? How do you implement the same functionality in 10g beside if <af:table> is used in 10g instead of <af:selectOneTable> etc...?

Posted by xchen on July 28, 2011 at 12:05 AM IST #

Hi Mohit,

I am using Web service Proxy in my application and facing same kind of issue you mentioned above. Did you found any solution for this issue?
Please let me know the solution if you found any.

Thanks
Pratap

Posted by guest on August 04, 2011 at 06:14 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Tips & Tricks from Arun on JDev ADF, Forms, SQL & PL/SQL.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today