2
votes

I have created a dataTable and associated context menu in backing bean but don't know how to configure rowSelect event listener. Here is part of the code:

DataTable table = (DataTable) application.createComponent(DataTable.COMPONENT_TYPE);
table.setValue(model);
table.setSelectionMode("single");
table.setVar("item");
table.setId("tableId");
                :

    ContextMenu ctxMenu = new ContextMenu();
    ctxMenu.setFor("tableId");

    DynamicMenuModel ctxModel = new DynamicMenuModel();

    ctxMenu.setModel(ctxModel);

    rightCenterPanel.getChildren().add(ctxMenu);
    rightCenterPanel.getChildren().add(table);

     RequestContext.getCurrentInstance().update(TreeManagedBean.rightCenterForm);

I am looking for equivalent of the following which can be added in backing bean:

<p:ajax event="rowSelect" listener="#{myBean.selectItem}"/>

Also is it possible to add single and double mouse click event listeners for dataTable in the backing bean without adding any " < p:ajax event" in the xhtml file?

1
1 Question per question please... "Also is it possible to add single and double mouse click event listeners for dataTable in the backing bean without adding any " < p:ajax event" in the xhtml file?" This is a new question. And please explain in that new question what kind of listeners you want. Pure client-side javascript?Kukeltje

1 Answers

2
votes

I am looking for equivalent of the following which can be added in backing bean:

<p:ajax event="rowSelect" listener="#{agentBean.selectItem}"/>

Use AjaxBehavior like this:

import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import org.primefaces.behavior.ajax.AjaxBehavior;
import org.primefaces.behavior.ajax.AjaxBehaviorListenerImpl;

final FacesContext fc = FacesContext.getCurrentInstance();
final ExpressionFactory ef = application.getExpressionFactory();
final MethodExpression me = ef.createMethodExpression(fc.getELContext(),
     "#{agentBean.selectItem}", String.class, new Class[0]);
final MethodExpression meArg = ef.createMethodExpression(fc.getELContext(),
     "#{agentBean.selectItem}", String.class, new Class[]{SelectEvent.class});
final AjaxBehavior ajaxBehavior = new AjaxBehavior();
ajaxBehavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(me, meArg));
dt.addClientBehavior("rowSelect", ajaxBehavior);

Provide an event handler method in your agentBean class:

public void selectItem(final SelectEvent event) {}

It will be called when a table row gets selected.

How it works

The event rowSelect gets linked to the AjaxBehavior on the DataTable. The AjaxBehavior registers a Listener which has the MethodExpression configured to call the agentBean.selectItem event handler method.