Sunday 17 November 2013

Getting Selected Row in a Table

Getting the selected Row in the managed bean.

1. The Table in the JSFF looks something like this. The table has a JSFF binding to a bean called "empBackingBean" introduced in the Backing Bean scope in the TF. Lets have an ActionListener in the Managedbean which displays the selected row in the table

<af:table value="#{bindings.VO1.collectionModel}"
                                        var="row"
                                        rows="#{bindings.VO1.rangeSize}"
                                        fetchSize="#{bindings.VO1.rangeSize}"
                                        rowBandingInterval="0"
                                        selectionListener="#{bindings.VO1.collectionModel.makeCurrent}"
                                        rowSelection="single" id="ATt5"
                                        columnStretching="column:c13"
                                        summary="#{myBundle.myTableName}"
                                        displayRow="selected"
                                        binding="#{backingBeanScope.empBackingBean.empTable}">
                                <af:column headerText="c1"
                                           id="c13">
                                   <af:outputText value="#{row.EmpId}"
                                                 id="ot1">
                                </af:column>
 <af:column headerText="c2"
                                           id="c14">
                                     <af:outputText value="#{row.EmpName}"
                                                 id="ot2">
                                </af:column>
<af:commandButton text="showSelectedCustomers 1" id="cb7"  actionListener="pageFlowScope.empManagedBean.showSelectedAL" />


2. The empManagedBean is the java bean that contains 'showSelectedAL'. The method gets the table from which is bound to the backing bean, gets the current row attributes. A custom method to evaluate the EL expression is also a part of the article

 public void showSelectedAL(ActionEvent actionEvent){
      empBackingBean requestBean= (empBackingBean ) evaluateELExp("#{backingBeanScope.empBackingBean }");
        RichTable empTable= requestBean.getEmpTable();
        JUCtrlHierNodeBinding currentEmployee=
            (JUCtrlHierNodeBinding)empTable.getSelectedRowData();
System.out.println("The Selected Employee "+currentEmployee.getAttribute("EmpName"));
// Write your custom logic here
}
public static Object evaluateELExp(String el) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory =
facesContext.getApplication().getExpressionFactory();
ValueExpression exp =
expressionFactory.createValueExpression(elContext, el,
Object.class);
return exp.getValue(elContext);
}


This is useful if the row selection is single. Multiple row selection can be handled by using empTable.getSelectedRowKeys(), but it is beyond the scope of this article.