Making a RemoteCommand programmatically
What is RemoteCommand
You can find a usage for PrimeFaces RemoteCommand on client side here. RemoteCommand is an implementation of UICommand in PrimeFaces library.
With RemoteCommand you can call a bean method as an ajax call from a client side component callback, for example onChange, onHide, onClick etc.
And in the following it is shown that how you can generate a RemoteCommand and join it to a component programmatically.
Required libraries
RemoteCommand example
Bean method
You can findComponentRecursivelyById method here
Please check this also
You can find a usage for PrimeFaces RemoteCommand on client side here. RemoteCommand is an implementation of UICommand in PrimeFaces library.
With RemoteCommand you can call a bean method as an ajax call from a client side component callback, for example onChange, onHide, onClick etc.
And in the following it is shown that how you can generate a RemoteCommand and join it to a component programmatically.
Required libraries
import java.util.Map; import javax.el.MethodExpression; import javax.faces.application.Application; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.event.MethodExpressionActionListener; import org.primefaces.component.outputlabel.OutputLabel; import org.primefaces.component.outputpanel.OutputPanel; import org.primefaces.component.remotecommand.RemoteCommand; import org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenu;
RemoteCommand example
FacesContext fc = FacesContext.getCurrentInstance(); Application app = FacesContext.getCurrentInstance().getApplication(); // generate a SelectCheckboxMenu SelectCheckboxMenu scm = (SelectCheckboxMenu) app.createComponent(SelectCheckboxMenu.COMPONENT_TYPE); String scmId = "scmId"; scm.setId(scmId); scm.setLabel("a caption.."); // call remote command named populateLabel from callback method // send the component id as a parameter to find the component at back-end scm.setOnHide("populateLabel([{name:'scmId', value:'" + scmId + "'}]);"); // prepare a remote command RemoteCommand remoteCommand = new RemoteCommand(); remoteCommand.setName("populateLabel"); MethodExpression methodExpression = app.getExpressionFactory().createMethodExpression( fc.getELContext(), "#{myBean.populateLabelOnHide}", null, new Class<?>[]{Object.class}); remoteCommand.addActionListener(new MethodExpressionActionListener(methodExpression)); remoteCommand.setUpdate(":form:" + scmId); remoteCommand.setProcess("@this form:" + scmId); // make output label OutputLabel outputLabel = (OutputLabel) app.createComponent(OutputLabel.COMPONENT_TYPE); outputLabel.setValue("SCM label"); outputLabel.setFor(scmId); // make an output panel and add components into it OutputPanel panel = (OutputPanel) app.createComponent(OutputPanel.COMPONENT_TYPE); panel.getChildren().add(scm); panel.getChildren().add(remoteCommand);
Bean method
public void populateLabelOnHide(Object actionEvent) { // find select checkbox menu component id parameter FacesContext context = FacesContext.getCurrentInstance(); Map<String, String> params = context.getExternalContext().getRequestParameterMap(); String selectCheckboxMenuId = params.get("scmId"); // find SelectCheckboxMenu component SelectCheckboxMenu scm = (SelectCheckboxMenu) findComponentRecursivelyById(selectCheckboxMenuId); // make an action on it scm.setLabel("Change the caption.."); }
You can findComponentRecursivelyById method here
Please check this also