Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces commandButton submitting on enter

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?

like image 687
c12 Avatar asked Mar 26 '11 09:03

c12


2 Answers

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" />
like image 68
Rock Solid Avatar answered Nov 03 '22 23:11

Rock Solid


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.

like image 25
Selvin Avatar answered Nov 04 '22 00:11

Selvin