Possible to execute `valueChangeListener` for `p:inputText` without hitting `enter` key?

前端 未结 1 612
无人及你
无人及你 2020-12-19 10:52

I\'d like to execute valueChangeListener for p:inputText when user changes text and inputText looses focus (onchange). Is this possible? For now it

相关标签:
1条回答
  • 2020-12-19 11:39

    The valueChangeListener method requires a form submit to be invoked. This is a server side event, not a client side event or so. Just changing and blurring the input does by default not submit the form at all. Bring in a <p:ajax> to do the magic.

    <p:inputText value="#{bean.inputValue}" valueChangeListener="#{bean.inputChanged}">
        <p:ajax />
    </p:inputText>
    

    However, although you didn't tell anything about the concrete functional requirement for which you thought that this is the right solution, I just wanted to mention that the valueChangeListener is more than often the wrong tool for the job you had in mind. Use <p:ajax listener> instead.

    <p:inputText value="#{bean.inputValue}">
        <p:ajax listener="#{bean.inputChanged}" />
    </p:inputText>
    

    Note that this would then make it possible to pass method arguments by EL 2.2, which would immediately then answer the possible underlying functional requirement of your other — actually pretty poor — question.

    <p:inputText value="#{bean.inputValue}">
        <p:ajax listener="#{bean.inputChanged('arg1', 'arg2')}" />
    </p:inputText>
    

    Also note that this might not be the solution at all if you're actually interested on the entered value; you could just access the inputValue property directly.

    See also:

    • When to use valueChangeListener or f:ajax listener?
    0 讨论(0)
提交回复
热议问题