5

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.

alexander
  • 1,191
  • 2
  • 20
  • 40
Adan
  • 147
  • 2
  • 2
  • 14
  • Sounds like you could use an answer from here http://stackoverflow.com/questions/9000669/re-enabled-pcommandbutton-not-firing-ajax – mrembisz Jan 31 '12 at 18:52

3 Answers3

16

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;
    }
}
Kushan
  • 10,657
  • 4
  • 37
  • 41
1

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.

alexander
  • 1,191
  • 2
  • 20
  • 40
0

If you are using JSF 2.0, you can accomplish this using the f:ajax tag.

You can disable a button with a condition in a bean

disabled="#{bean.isDisabled}"

when you rerender the button when you click on button 1, the button can be enabled.

alexander
  • 1,191
  • 2
  • 20
  • 40
cremersstijn
  • 2,375
  • 4
  • 28
  • 41