55 votes

Passer le paramètre à p:remoteCommand à partir de JavaScript

Je veux transmettre la valeur à remoteCommand partir de javascript. Si cela est possible, comment puis-je le faire et comment puis-je les recevoir dans le haricot de soutien ?

62voto

Joel Points 1087

Page:

 <p:remoteCommand name="command" action="#{bean.method}" />

JavaScript :

 command({param: 'value'});

Haricot:

 public void method() {
    String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("param");
}

27voto

Cagatay Civici Points 3335
remoteCommandFunctionName({name1:'value1', name2:'value2'});

9voto

Jonathan L Points 1

Combinez @BalusC @Joel's post pour un exemple fonctionnel

 <h:form>
    <p:remoteCommand name="rcName" update="msgs" actionListener="#{remoteCommandView.beanMethod}" />
    <p:growl id="msgs" showDetail="true" />

    <p:commandButton type="button" onclick="rcName([{name:'model', value:'Buick Encore'}, {name:'year', value:2015}]);" value="Pass Parameters 1" /><br/>
    <p:commandButton type="button" onclick="clicked();" value="Pass Parameters 2" />
</h:form>

<script type="text/javascript">
    //<![CDATA[
    function clicked(){
        rcName([{name:'model', value: 'Chevy Volt'}, {name:'year', value:2016}]);
    }
    //]]>
</script>
 @ManagedBean
public class RemoteCommandView {
    public void beanMethod() {
        // OR - retrieve values inside beanMethod
        String model1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("model");
        String year1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("year");
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Executed", 
            "Using RemoteCommand with parameters model := " + model + ", year := " + year));
    }

    @ManagedProperty("#{param.model}")
    private String model;

    @ManagedProperty("#{param.year}")
    private int year;

    public void setModel(String model) {
        this.model = model; // value set by JSF
    }

    public void setYear(int year) {
        this.year = year;
    }
}

4voto

Lorsque vous devez passer plus d'un paramètre à partir de javascript, la syntaxe est :


remoteCommandFunction([{name:'param1', value: param1 }, {name:'param2',value: param2 }, {name:'param3',value: param3 }]);

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X