I have two commandButtons and when I hit enter the first one submits. I really only want to submit the second button if the user hits enter. Any ideas?
I realize that the question is old, but maybe it is still interessing for someone to see a different solution.
You can use the Primefaces defaultCommand and adding an id to your prefered Button to define what happens when the Enter key is pressed. That's all.
<p:commandButton value="button1" action="#{bean.action1}"/>
<p:commandButton value="button2" id="btn_to_enter" action="#{bean.action2}"/>
<p:defaultCommand target="btn_to_enter" />
You have to swap the position of those two buttons thats all.
Your current code should be.
<p:commandButton value="button1" action="#{bean.action1}"/>
<p:commandButton value="button2" action="#{bean.action2}"/>
By default the button1 action will be triggered. You can present the user an alternative view, by adding style="float:right" to the button1.
<p:commandButton value="button1" style="float: right" action="#{bean.action2}"/>
<p:commandButton value="button2" action="#{bean.action1}"/>
Using the above the button1 will appear after the button2, and perform the action of button2, whenever the Enter is pressed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With