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
Hi. I tried the code to get selected rowes in button click, but there are the following three errors.
ReplyDelete1) 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??
Ajanth
DeleteYou should download the sample application and then check it, you'll get all variables and beans there
Ashish
Hi,
ReplyDeleteDo you have example POJO based ADF table with delete function ? Thanks
You can check this blog by Timo Hahn - Delete Row from POJO based Table
DeleteAshish
I need to do this in ListView(with POJO). How can i do? thanks you
ReplyDeleteYou can use same method for listView too as af:table and af:listView share same structure
DeleteI did this:
Deletepublic 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);
}
Have you tried method mentioned in blog ?
DeleteHow can i select the first row, after deleting any row in table in case of row selection single.
ReplyDeletethank you.
Pojo variables are not recognizing in af:table rows?
ReplyDeleteTy
how can i get pojo variables to af:table?
DeleteAre you trying to populate a table using POJO ?
DeleteOr trying to get selected row ?