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