Showing posts with label JSF. Show all posts
Showing posts with label JSF. Show all posts

January 10, 2016

How to set multiple Default Command using Primefaces 5

Primefaces 5, gives a feature to allow multiple defaultcommand (ENTER) within a form.
Default Command can be use to define which commandbutton should be triggered when the enter key is pressed.

Following is the code for defining defaultCommand in primefaces.

 <p:defaultCommand target="buttonId" />  
target is the identifier of commandbutton that needs to be triggered.

Now, to allow mutiple command buttons triggering within the same form we need to use scope attribute in the same tag as below:

 <p:defaultCommand target="buttonId" scope="panelgrid_id" />  
 Example,  
 <h:panelGrid columns="2" cellpadding="5" id="panelgrid1">   
      <h:outputLabel for="btnSelect" value="Button 1:" />   
 ................  
 ................  
 <p:commandButton value="Button1" id="btn1" actionListener="#{defaultCommandView.btn1Submit}" />   
 <p:defaultCommand target="btn1" scope="panelgrid1" />  
 </h:panelGrid>   
 <p:separator />   
 <h:panelGrid columns="3" cellpadding="5" id="panelgrid2">   
 <h:outputLabel for="btnSelect" value="Button 1:" />   
 ................  
 ................  
 <p:commandButton value="Button2" id="btn2" actionListener="#{defaultCommandView.btn2Submit}" />   
 <p:defaultCommand target="btn2" scope="panelgrid2" />  
 </h:panelGrid>  
There are 2 default command tags been used in above code so, if user has any input text in focus under panel grid one then it on press on ENTER key then it will trigger command button one. Similarly for command button second.