1
<h:form>   
  <p:autoComplete id="autoCompleteID" value="#{myBean.item}"  
                       completeMethod="#{myBean.completeMethod}">   
  </p:autoComplete>
  <h:commandButton action="#{myBean.searchRelatedItems}"/>
</h:form>

Here my scenario is like Google search, I can see related Items in suggestion and also redirect another page based on text typed in p:autoComplete text field. Its works fine by clicking the button, but I also want to achieve the same by hitting ENTER in p:autoComplete text field.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Sagar Koshti
  • 408
  • 2
  • 6
  • 15
  • possible duplicate of [Submit form on enter when in text field](http://stackoverflow.com/questions/4418819/submit-form-on-enter-when-in-text-field) – Aritz Jan 16 '15 at 12:41
  • Not a duplicate. I believe the title should contain "Primefaces autocomplete" as this is no regular form submit. – Lenymm Dec 02 '15 at 20:13
  • Does this answer your question? [Default action to execute when pressing enter in a form](https://stackoverflow.com/questions/5485851/default-action-to-execute-when-pressing-enter-in-a-form) – Jasper de Vries Mar 07 '23 at 17:39

1 Answers1

0

You can achieve this with the defaultCommand

<h:form>   
  <p:autoComplete id="autoCompleteID" value="#{myBean.item}"  
                       completeMethod="#{myBean.completeMethod}"    
  </p:autoComplete>
  <h:commandButton id="button" action="#{myBean.searchRelatedItems}"/>
  <p:defaultCommand target="#{myBean.btn}" />
</h:form>

and in your bean, a property with getter/setters idicating a button to be pressed on ENTER

 private String btn = "button";
Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • It worked but for that i need to press ESC key before pressing ENTER key. It doesn't works when suggestion list is open, from auto complete. – Sagar Koshti Jan 17 '15 at 04:58
  • Have you actualy resolved this issue? I just ran into it and it's pretty annoying. Pressing enter when the autocomplete overlay is up does nothing unless you select an item which I don't want to. All I want for the autocomplete to behave like Google search. – Lenymm Dec 02 '15 at 20:15
  • Using onkeypress and checking for enter key doesn't work either. The autocomplete overlay is most likely eating all the keyboard input. – Lenymm Dec 02 '15 at 20:24