Monday, July 23, 2012

How to get a selected row from af:table component in backing bean

Hello,
  This might be very common requirement for any ADF developer , to get a selected row(s) from <af:table> component in a backing bean.

// Table Binding
<af:table value="#{bindings.EmpVO.collectionModel}" var="row"
          rows="#{bindings. EmpVO .rangeSize}"
          emptyText="#{bindings. EmpVO .viewable ? 'No data to display.' : 'Access Denied.'}"
          fetchSize="#{bindings. EmpVO .rangeSize}"
          rowBandingInterval="0"
          rowSelection="multiple" id="t1"
          styleClass="AFStretchWidth" columnSelection="multiple"
          first="0" contentDelivery="immediate" autoHeightRows="10"
          binding="#{pageFlowScope.ExampleBean.employeeTable}">

Below approach is advisable, If multiple selection rows required 

       

// Get the instance for table component in backing bean
UIXTable table = getEmployeeTable();
// Get the Selected Row key set iterator
Iterator selectionIt = table.getSelectedRowKeys().iterator();
while(selectionIt.hasNext()){
Object  rowKey = selectionIt.next();
 table.setRowKey(rowKey);
 int index = table.getRowIndex(); 
      FacesCtrlHierNodeBinding row =
        (FacesCtrlHierNodeBinding) table.getRowData(index);
Row selectedRow = row.getRow();
}
       
 


-- Below approach is advisable if there is only single row selection is enabled.


       

// Get the instance for table component in backing bean
UIXTable table = getEmployeeTable();
// Get the Selected Row key set iterator
Iterator selectionIt = table.getSelectedRowKeys().iterator();
while(selectionIt.hasNext()){
Object  rowKey = selectionIt.next();
table.setRowKey(rowKey); int index = table.getRowIndex();
FacesCtrlHierNodeBinding row = (FacesCtrlHierNodeBinding) table.getRowData(index);
Row selectedRow = row.getRow();
}
       
 




2 comments:

  1. Good to see your blog Ravi. To collaborate on the same.


    One realization was these approaches works fine with the View Objects, as we are setting the primary key. But when a webservice proxy is used, dropping the response datacontrol to the page, the selected row returns the first always row!

    One way to overcome is have to get the pTable.getSelectedRowKeys() and iterate to get the key and set it as RowIndex of the CollectionModel.

    http://arunswebcenterstunts.wordpress.com

    ReplyDelete
  2. Is there a difference between the two blocks of code (for multiple or sigle row selection enabled). I don't see any. I think you trolled me here!!

    ReplyDelete