Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable / disable a JSF Command Button [duplicate]

Tags:

jsf

primefaces

The scenario looks like this:

There are two buttons in the screen, say button1 and button2. On page load, button2 is disabled. When the user clicks button1, button2 will be enabled. I achieved this functionality using javascript window.onload event, and onclick event on button1. My problem is, when the user clicks button1 it triggers a page reload. With that being said, after the reload of the page, button2 is disabled because of the onload event.

Is there another way to achieved this functionality?

By the way I'm using primefaces.

like image 310
Adan Avatar asked Jan 31 '12 13:01

Adan


2 Answers

primeface page goes like this,

 <p:commandButton update="panel1"  actionListener="#{bean.button1}" value="button1" disabled="#{bean.disable}"> 
     <f:setPropertyActionListener value="#{false}" target="#{bean.disable}"/>   
 </p:commandButton> 


 <p:commandButton update="panel2"  actionListener="#{bean.button2}" value="button1" disabled="#{!(bean.disable)}"> 
     <f:setPropertyActionListener value="#{true}" target="#{bean.disable}"/>    
 </p:commandButton> 

Manage Bean:

 public class Bean {

    private boolean disable;

    // default constructor 
    public Bean(){
       this.disable= false;
    }

    public boolean isDisable() {
       return disable;
    }
    public void setDisable(boolean disable) {
       this.disable = disable;
    }
}
like image 68
Kushan Avatar answered Nov 07 '22 23:11

Kushan


There is a lot easier way in my opinion.

Just create a boolean property (with getter + setter) in a managed bean with javax.faces.bean.ViewScoped and javax.faces.bean.ManagedBean annotated.

You can handle the disabled state via this property (button2State).

<h:form>
    <p:commandButton id="button1" update="button2"
        action="#{bean.setButton2State(false)}"
        value="Enable second button" />
    <p:commandButton id="button2" disabled="#{bean.button2State}"
        value="button disabled one page load" />
</h:form>

The first button calls the setter of the property and set button2State to false to enable the button. Also this example uses ajax to update the button only.

like image 38
alexander Avatar answered Nov 07 '22 23:11

alexander