Tuesday, April 1, 2014

How to Mark Selected row in as current row in SelectionListener ?

This blog explains about how to make the selected row in <af:table> component  as a current row Progromatically using selection Listener

       
    
 import javax.el.ELContext;
 import javax.el.ExpressionFactory;
 import javax.el.MethodExpression;
 import javax.el.ValueExpression;

 import javax.faces.context.FacesContext;

 import oracle.jbo.Row;

 import org.apache.myfaces.trinidad.event.SelectionEvent;

    /**
     * Programmatic invocation of a method that an EL evaluates to.
     *
     * @param el EL of the method to invoke
     * @param paramTypes Array of Class defining the types of the parameters
     * @param params Array of Object defining the values of the parametrs
     * @return Object that the method returns
     */
    private  Object invokeEL(String el, Class[] paramTypes, Object[] params) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
        MethodExpression exp = expressionFactory.createMethodExpression(elContext, el, Object.class, paramTypes);

        return exp.invoke(elContext, params);
    }

    public Object evaluateEL(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 method invoke whenever any row selected in  component
     * @param selectionEvent
     */
    public void empSelListener(SelectionEvent selectionEvent) {
        // Below is the code to make the selected row as current row
        invokeEL("#{bindings.EmployeesView1.collectionModel.makeCurrent}", new Class[] { SelectionEvent.class }, new Object[] {
                         selectionEvent });
        Row selectedRow = (Row)evaluateEL("#{bindings.EmployeesView1.currentRow}");
        System.out.println(selectedRow.getAttribute("LastName"));
    }
       
 

- Ensure to override the selectionListener attribute to a backing bean method as shown below
 - Below is the <af:table> component, I have selected '102' row
 - Displays the selected row corresponding last name



No comments:

Post a Comment