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:

Great post. I am searching for a few days for this solution. But I am using JDEV 10.1.3.4.0, do you have the code for the method:

public String printSelectedEmpNames()

in 10g?

Thanks.

Posted by shawn on July 27, 2011 at 05:50 PM IST #

Hello Arunkumar,

we are experience the same problem here in our project. BTW we found a sever coincidence, with the selectedRowKeys property removed the selection wont become "re-set" after there was an update to the model (in the background).
We already filed a service request here to Oracle that they implement something like:
selectedRowKeys="#{bindings.EmpView1.collectionModel.selectedRowS}"

(see the big S at the end). I think they map to a real list iterator internally and that can only have one "selected" item.

Posted by Johannes Bayer on August 24, 2011 at 04:25 PM IST #

@Shawn
>Great post. I am searching for a few days for this solution. But I am >using JDEV 10.1.3.4.0, do you have the code for the method:

>public String printSelectedEmpNames()

>in 10g?

>Thanks.
Currently I do not have a example for 10.1.3 version.

Posted by Arun on September 16, 2011 at 12:43 PM IST #

@Johannes
Thanks for this information. :).

Posted by Arun on September 16, 2011 at 12:46 PM IST #

I just came here to THANK YOU!!

Thank God you exist m8!!
I spent hours trying to debug the reason it only selected the latest row but thanks you, it works 100%.

Thanks very much and have a good day!!!

Posted by FredericoSB on October 23, 2013 at 08:24 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Tips and Tricks from Oracle's JDeveloper & ADF QA

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