How to enable/disable primefaces commandButton?

28,569

Solution 1

Try to update the second button on clicking the first one like the dataTable


You should replace the bean in disabled="#{!(bean.disable)}"> with mainTable => disabled="#{!(mainTable.disable)}">

Solution 2

You should swap 2 buttons in a Outputpanel, and update this outputpanel alter for next button, like this:

<p:outputPanel id="pnltest">
  <p:commandButton value="Normalize"
    actionListener="#{mainTable.normalize}" update="dataTable,pnltest"
    ...
  </p:commandButton>
  <p:commandButton value="To Verify Next->" action="verify.xhtml"
    actionListener="#{mainTable.verify}" id="next"
    ...
  </p:commandButton>
<p:outputPanel>
Share:
28,569
yetAnotherSE
Author by

yetAnotherSE

Software Engineer Blog

Updated on May 29, 2020

Comments

  • yetAnotherSE
    yetAnotherSE almost 4 years

    I have two buttons on screen. When page first loaded I want to button2 is disabled until button1 is clicked. When button1 is clicked, button2 must be enabled.

    I tried:

    <p:commandButton value="Normalize"
        actionListener="#{mainTable.normalize}" update="dataTable"
        id="normalize" styleClass="ui-priority-primary"
        style="font-size: 14px">
        <f:setPropertyActionListener value="#{true}"
            target="#{mainTable.disable}" />
    </p:commandButton>
    <p:commandButton value="To Verify Next->" action="verify.xhtml"
        actionListener="#{mainTable.verify}" id="next"
        styleClass="ui-priority-primary" style="font-size: 14px"
        disabled="#{!(bean.disable)}">
    </p:commandButton>
    

    My bean:

    @ManagedBean
    @SessionScoped
    public class MainTable
    {
    
        private boolean disable;
    
        public MainTable()
        {
            disable = false;
        }
        public boolean isDisable()
        {
            return disable;
        }
    
        public void setDisable(boolean disable)
        {
            this.disable = disable;
        }
    }
    

    But it doesn't work. When I clicked button1, button2 is still disabled. What is wrong?