Please disable your adblock and script blockers to view this page

Search this blog

Thursday, 16 July 2015

Get selected row (single/multiple) from POJO based table in ADF


Previously i have posted about populating af:table from managed bean using POJO and adding records in it, Check this-
Populate af:table programmatically from managead bean using POJO 
In this post i am extending previous post application
This post is about getting selected row from POJO based table , It is a common requirement while using af:table based on POJO

Get single row using custom selection listener-


SelectionListener  handles selection event of table , whenever user selects a row in table ,selection listener is fired
Set table RowSelection property to single (to select one row at a time) or multiple (to select multiple row ) and create a custom selection listener in managed bean that will handle table's selection event




See selection listener code in managed bean we can get selected row  using this -



import oracle.adf.view.rich.component.rich.data.RichTable;
import org.apache.myfaces.trinidad.event.SelectionEvent;

 /**Method to get selected row(single)
     * @param selectionEvent
     */
    public void tableSelection(SelectionEvent selectionEvent) {
        //Get table from selectionEvent
        RichTable richTable = (RichTable) selectionEvent.getSource();
        //Cast to the List that populates table
        PersonBean row = (PersonBean) richTable.getSelectedRowData();
        //Get the attributes (column) from list
        System.out.println(row.getName());
    }

Now check this -

 Output on console :)

Get single/multiple selected row on a button click (ActionEvent)-


Set table RowSelection to multiple and select multiple row using Ctrl key of your keyboard
and check this code to get multiple selected row using RowKeySet, We can get all row using getSelectedRowKeys method


import org.apache.myfaces.trinidad.model.RowKeySet;
import java.util.Iterator;    



/**Method to get all selected record in af:table
     * @param actionEvent
     */
    public void getSelectedRecord(ActionEvent actionEvent) {
        //getTableBind is binding of table on page.
        RowKeySet selectedEmps = getTableBind().getSelectedRowKeys();
        //Create iterator from RowKeySet
        Iterator selectedEmpIter = selectedEmps.iterator();

        while (selectedEmpIter.hasNext()) {
            String i = selectedEmpIter.next().toString();
            //personList is the list used to populate table and name is a column of table
            //So here filter list using index and get values then
            PersonBean rowIter = personList.get(Integer.parseInt(i));
            System.out.println(rowIter.getName());
        }
       
    }

Now run and check again -

 Output on console :)

Sample ADF Application- Download
Cheers :) Happy Learning

12 comments :

  1. Hi. I tried the code to get selected rowes in button click, but there are the following three errors.

    1) Method getTableBind() is not found
    2) Type PersonBean not found
    3) Type or Variable personList not found.

    What changes should I make or libraries should I include to fix this issue??

    ReplyDelete
    Replies
    1. Ajanth

      You should download the sample application and then check it, you'll get all variables and beans there

      Ashish

      Delete
  2. Hi,
    Do you have example POJO based ADF table with delete function ? Thanks

    ReplyDelete
  3. I need to do this in ListView(with POJO). How can i do? thanks you

    ReplyDelete
    Replies
    1. You can use same method for listView too as af:table and af:listView share same structure

      Delete
    2. I did this:
      public void seleccionarEvento(SelectionEvent selectionEvent) {
      List event = (List)Util.getValuePageFlowScope("eventos");
      RichListView lst = (RichListView)selectionEvent.getComponent();
      RowKeySet rowKey = lst.getSelectedRowKeys();
      int sel =(Integer)rowKey.toArray()[0];
      Eventos.Evento evento=(Eventos.Evento) event.get(sel);
      Util.setValuePageFlowScope("eventoSeleccionado", evento);
      }

      Delete
    3. Have you tried method mentioned in blog ?

      Delete
  4. How can i select the first row, after deleting any row in table in case of row selection single.

    thank you.

    ReplyDelete
  5. Pojo variables are not recognizing in af:table rows?
    Ty

    ReplyDelete
    Replies
    1. how can i get pojo variables to af:table?

      Delete
    2. Are you trying to populate a table using POJO ?
      Or trying to get selected row ?

      Delete