3

I' have seen many answers here but none worked for what I want, I need to do this in a way that the form is not submited, and by using the hidden button approach (with an ajax call) and calling the click() event from javascript, the method is called, but I need to pass an int in the method, how can I do that?

I can get the int from a javascript var or from a hidden input text field, just don't know how to do that, can anyone help with that?

JSF code:

<script type="text/javascript" language="javascript">
    var position;
</script>

<h:inputHidden id="hiddenHolder" value="#{backingBean.beanPosition}" />

<h:commandButton id="hiddenButton" style="display: none;">
    <f:ajax listener="#{backingBean.testMethod(need to send here javascript var or inputHidden value)}"/>
</h:commandButton>

Bean function;

public void testMethod(int i){
    //do stuff
}

When I change the hiddenHolder value from javascript that is not reflected in the backingBean, I guess it needs a submit for that to work. That is why I need to pass the value in the method call.

Mikel
  • 1,581
  • 17
  • 35

2 Answers2

1

When I change the hiddenHolder value from javascript that is not reflected in the backingBean

You're indeed not telling JSF to process the input value. The <f:ajax execute> should be used for this, which defaults to @this (the current input or command component). As the <f:ajax> is enclosed inside a command component, only the command component itself (its action) will be processed.

So, just explicitly specify the input value along with the command component:

<f:ajax execute="@this hiddenHolder" ... />

Or, if this all is within the same form, use @form:

<f:ajax execute="@form" ... />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Ok, with this the values are now updated in the bean, so I no longer need to send them as parameters of the called function, thanks. But I still can not reload a specific component after changing the values in the bean. I tried to use the render atribute in the ajax call with the ids of the components to update, but nothing changes. Do I need to prepend the form id in each id? I have everything in the same form. – Mikel Feb 05 '15 at 12:59
  • This does not exactly answer my question, but removes the need to it in the first place. All is working correctly now, thanks for pointing in the correct direction. – Mikel Feb 25 '15 at 15:39
0

Use the OmniFaces commandScript for this. Works great (or use the PrimeFaces remoteCommand if you already use that)

To prevent the rest of the form being submitted, use the execute attribute like with an f:ajax call (process attribute on p:remoteCommand) or use separate forms

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Can you give me an example for the use of commandScript with the code I provided in the question? Been trying myself but keep getting the form submited, and I can't do that. – Mikel Feb 04 '15 at 17:15
  • Use a separate form or use the execute attribute (enhanced my answer) – Kukeltje Feb 04 '15 at 17:18