Question:
Is it possible to pass values to a PrimeFaces p:remoteCommand from JavaScript, and how can it be done?
Answer:
Yes, it is possible to pass parameters to a p:remoteCommand from JavaScript. The syntax and approach depend on the PrimeFaces version:
Since PrimeFaces 3.3, the syntax has changed as follows:
<code class="javascript">increment([{name:'x', value:10}, {name:'y', value:20}]);</code>
This allows for passing multiple values for a parameter name. Single-valued parameters can still be accessed as before:
@ManagedProperty("#{param.x}") private int x; @ManagedProperty("#{param.y}") private int y;
Before PrimeFaces 3.3, the syntax was:
<code class="javascript">increment({param1:'val1', param2:'val2'});</code>
In the backing bean, parameters can be accessed through:
@ManagedProperty("#{param.param1}") private String param1; @ManagedProperty("#{param.param2}") private String param2;
In PrimeFaces 3.3 or newer, it is possible to specify parameters with multiple values, as in:
<code class="javascript">functionName([{name:'foo', value:'one'}, {name:'foo', value:'two'}, {name:'foo', value:'three'}]);</code>
These parameters can be accessed in a request scoped bean:
@ManagedProperty("#{paramValues.foo}") private String[] foos;
Or in a method of a broader scoped bean:
<code class="java">Map<String, String[]> paramValues = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap(); String[] foos = paramValues.get("foo");</code>
Additional Resources:
The above is the detailed content of How to Pass Values to p:remoteCommand from JavaScript in PrimeFaces?. For more information, please follow other related articles on the PHP Chinese website!