Monday, May 19, 2014

Get the current selected row of af:table

In order to get the selected row attributes of af:table, follow these steps:


1. Bind the selectionListener property to a java method as:

<af:table value="#{bindings.EmployeesVO1.collectionModel}" var="row"
                  rows="#{bindings.EmployeesVO1.rangeSize}"
                  emptyText="#{bindings.EmployeesVO1.viewable ? 'No data to display.' : 'Access Denied.'}"
                  fetchSize="#{bindings.EmployeesVO1.rangeSize}"
                  rowBandingInterval="0"
                  selectedRowKeys="#{bindings.EmployeesVO1.collectionModel.selectedRow}"
                  selectionListener="#{EmployeeEdit.employeeSelectListener}"
                  rowSelection="single" id="t1">

2. Define the selectionListener in managed bean as:

    public void employeeSelectListener(SelectionEvent selectionEvent) {
        System.out.println("Selection event called");
     
        DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding dcItteratorBindings =bindings.findIteratorBinding("EmployeesVO1Iterator");
        GenericTableSelectionHandler.makeCurrent(selectionEvent);
        // Get an object representing the table and what may be selected within it
        ViewObject voTableData = dcItteratorBindings.getViewObject();
        
        // Get selected row
        Row rowSelected = voTableData.getCurrentRow();
        if(rowSelected.getAttribute("EmployeeId")!=null){
            System.out.println("Employee selecetd"+rowSelected.getAttribute("EmployeeId")+"    "+rowSelected.getAttribute("FirstName"));   
        }
    }


3. You need to create a custom class that would point the selection event to the current row, else no matter what is the selection the 1st row would be returned. Code for makeCurrent is as follows:

    public static void makeCurrent( 
    SelectionEvent selectionEvent){ 
    
    RichTable _table = (RichTable) selectionEvent.getSource(); 
    //the Collection Model is the object that provides the 
    //structured data 
    //for the table to render 
    CollectionModel _tableModel = (CollectionModel) _table.getValue(); 
    //the ADF object that implements the CollectionModel is 
    //JUCtrlHierBinding. It is wrapped by the CollectionModel API 
    JUCtrlHierBinding _adfTableBinding = (JUCtrlHierBinding) _tableModel.getWrappedData(); 
    //Acess the ADF iterator binding that is used with 
    //ADF table binding 
    DCIteratorBinding _tableIteratorBinding = _adfTableBinding.getDCIteratorBinding(); 
    
    //the role of this method is to synchronize the table component 
    //selection with the selection in the ADF model 
     Object _selectedRowData = _table.getSelectedRowData(); 
     //cast to JUCtrlHierNodeBinding, which is the ADF object 
     //that represents a row 
     JUCtrlHierNodeBinding _nodeBinding = 
     (JUCtrlHierNodeBinding) _selectedRowData; 
     //get the row key from the node binding and set it 
     //as the current row in the iterator 
     Key _rwKey = _nodeBinding.getRowKey(); 
     _tableIteratorBinding.setCurrentRowWithKey( 
     _rwKey.toStringFormat(true)); 
     } 

4. Run the code to see the output:





10 comments:

  1. Thanks! is GenericTableSelectionHandler the custom Class you created?

    ReplyDelete
  2. Thanks Amitt. This solution worked perfect for me.
    Regards,
    Rakesh.

    ReplyDelete
  3. Which package do we need to import for accessing class Key?

    ReplyDelete
  4. Thankful. That's exactly my problem

    ReplyDelete
  5. Which package do we need to import for accessing CollectionModel and JUCtrlHierBinding
    ?

    ReplyDelete
    Replies
    1. There you go. List of all the imports you need:


      import oracle.adf.model.binding.DCIteratorBinding;
      import oracle.adf.view.rich.component.rich.data.RichTable;
      import oracle.jbo.Key;
      import oracle.jbo.uicli.binding.JUCtrlHierBinding;
      import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;
      import org.apache.myfaces.trinidad.event.SelectionEvent;
      import org.apache.myfaces.trinidad.model.CollectionModel;

      Delete
  6. can you please share the code for
    GenericTableSelectionHandler.makeCurrent class .. i am confusing , where to define..above class

    ReplyDelete
    Replies
    1. You may choose your package to place the class and import its accordingly. Here is the class:


      import oracle.adf.model.binding.DCIteratorBinding;
      import oracle.adf.view.rich.component.rich.data.RichTable;
      import oracle.jbo.Key;
      import oracle.jbo.uicli.binding.JUCtrlHierBinding;
      import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;
      import org.apache.myfaces.trinidad.event.SelectionEvent;
      import org.apache.myfaces.trinidad.model.CollectionModel;

      public class GenericTableSelectionHandler {

      public GenericTableSelectionHandler() {
      super();
      }

      /**
      * Synchronizes the table UI component row selection with the
      * selection in the ADF binding layer
      * @param selectionEvent event object created by the table
      * component upon row selection
      */
      public static void makeCurrent(
      SelectionEvent selectionEvent){

      RichTable _table = (RichTable) selectionEvent.getSource();
      //the Collection Model is the object that provides the
      //structured data
      //for the table to render
      CollectionModel _tableModel = (CollectionModel) _table.getValue();
      //the ADF object that implements the CollectionModel is
      //JUCtrlHierBinding. It is wrapped by the CollectionModel API
      JUCtrlHierBinding _adfTableBinding = (JUCtrlHierBinding) _tableModel.getWrappedData();
      //Acess the ADF iterator binding that is used with
      //ADF table binding
      DCIteratorBinding _tableIteratorBinding = _adfTableBinding.getDCIteratorBinding();

      //the role of this method is to synchronize the table component
      //selection with the selection in the ADF model
      Object _selectedRowData = _table.getSelectedRowData();
      //cast to JUCtrlHierNodeBinding, which is the ADF object
      //that represents a row
      JUCtrlHierNodeBinding _nodeBinding =
      (JUCtrlHierNodeBinding) _selectedRowData;
      //get the row key from the node binding and set it
      //as the current row in the iterator
      Key _rwKey = _nodeBinding.getRowKey();
      _tableIteratorBinding.setCurrentRowWithKey(
      _rwKey.toStringFormat(true));
      }

      }

      Delete