3

I am trying to update my form on the basis of changes of the spinner

<p:spinner id="spin1" min="1" max="#{editPhoto.max}" size="1" 
           value="#{editPhoto.page}" validator="#{editPhoto.validator()}" 
           valueChangeListener="#{editPhoto.refreshForm()}" />
<p:commandButton value="Insert" action="#{editPhoto.insertImage()}"  />
<p:commandButton value="Delete" action="#{editPhoto.deleteImage()}" />

I've put break points in the value, setPage as well as in the validator and valueChangeListener and it hits them only when I press the commandButton. I've tried immediate="true", but that adds nothing. What I really want is to know when the value has been changed, but without having to hit the commandButton.

In a previous question BalusC suggested the use of

<f:event type="preRenderView" listener="#{nextpageBacking.onPreRenderView}" />

I suspect that I need something similar here, but what sort of event should I be looking for? Maybe not, so what do I need to do to get changes in the spinner without having to press on the commandButton? Thanks, Ilan

My bean is view-scoped. Perhaps the answer is to use another request scoped-bean and have the request scoped-bean operate on the view-scoped bean? I will try this if this looks like the correct direction.

Community
  • 1
  • 1
Ilan Tal
  • 509
  • 2
  • 8
  • 22

1 Answers1

4

You should use f:ajax or p:ajax.

The valueChangeListener will only fire when the whole form or the spinner is submitted.

<p:spinner id="spin1" min="1" max="#{editPhoto.max}" size="1" 
           value="#{editPhoto.page}" validator="#{editPhoto.validator()}">
    <p:ajax listener="#{editPhoto.refreshForm()}" 
            update="@form" process="@this" />
</p:spinner>

The valueChangeListener attribute of the spinner is not the best place to fire your method. You should better put it in the listener attribute of p:ajax. The valueChangeListener should be used if you are interested in the old and the new value.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • In addition, there are examples in the showcase of Primefaces, one is for Ajax Spinner : http://www.primefaces.org/showcase/ui/spinner.jsf – La Chamelle Jun 06 '12 at 11:33
  • Absolutely beautiful. It is all so simple when you know the answer. I will remove the validator which existed just so I could catch a break point. Also I see you update the form which I what my bean refreshForm was supposed to do. Perhaps I don't even need the listener in this case. Thanks for the beautiful answer. – Ilan Tal Jun 06 '12 at 11:47
  • 1
    Notice that the `process` attribute is set to `@this` in my example and the spinner change will thus only reflect changes in the spinner itself. But you can change it to `@form` as well. Normally you don't need the listener function for only refreshing the form. – Matt Handy Jun 06 '12 at 11:49
  • Great example... you really help me with this issue! Thanks – jpganz18 Jan 08 '13 at 23:19