Tuesday Dec 14, 2010

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.

[Read More]
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