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.
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;
}
}
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.
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