Mirco Attocchi
Mirco Attocchi

Reputation: 801

p:inputText onchange do p:commandButton

What 's the best way to execute a commandButton on the onchange event of an inputText?

I'm trying to build a dynamic search result while user digit string.

            <p:inputText id="txtSearch" value="#{contattiDitteController.search}">
            </p:inputText>

            <p:commandButton id="btnSearch" value="Search" action="#{contattiDitteController.actionSearch}" update="pnlResult"/>

I'm using this, but is the best way (or I can do better with primeface)?

            <script>
                function refresh() {
                    jQuery('[id$="btnSearch"]').click();
                }
            </script>

            <p:inputText id="txtSearch" value="#{contattiDitteController.search}" onkeypress="refresh();"/>

            <p:commandButton id="btnSearch" value="Search" action="#{contattiDitteController.actionSearch}" update="pnlResult"/>

Upvotes: 3

Views: 18712

Answers (1)

BalusC
BalusC

Reputation: 1108632

Use <p:ajax>.

<p:inputText id="txtSearch" value="#{contattiDitteController.search}">
    <p:ajax event="keyup" listener="#{contattiDitteController.actionSearch}" update="pnlResult" />  
</p:inputText>

See also the showcase.

Upvotes: 10

Related Questions