angus
angus

Reputation: 3320

Unable to disable <p:commandButton> via ajax

I want to disable the button in case there is no value in the input text something like this:

<p:autoComplete  id="ac" value="#{bean.selectedNet}"
    completeMethod="#{bean.complete}"
    minQueryLength="0" size="3">
    <p:ajax event="itemSelect" update="genButton" listener="#{bean.handleNetChange}"/>
    <p:ajax process="@this" update="genButton" />
</p:autoComplete> 

<p:selectManyCheckbox id="en" layout="pageDirection" value="#{bean.selectedEntity}"> 
    <f:selectItems value="#{bean.entityList}"/>
    <p:ajax update="genButton" listener="#{bean.handleNetChange}"/>
</p:selectManyCheckbox>

<p:commandButton id="genButton" value="Generate Files"
    widgetVar="startButton1"
    disabled="#{bean.disableButton}"
    actionListener="#{bean.generate}"
    onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)"
    ajax="false">
    <p:fileDownload value="#{bean.streamedContent}"/>
</p:commandButton>

Methods:

public void handleNetChange() {
    if (!StringUtils.isBlank(selectedNet) && !selectedEntity.isEmpty()) {
        disableButton = false;
    } else {
        disableButton = true;
    }
}

public List<String> complete(String query) {
    List<String> results = new ArrayList<String>();
    if (StringUtils.isBlank(query)) {
        selectedNet = query;
        disableButton = true;
    } else {
        ....
    }
    return results;
}

While the itemSelect event is working perfectly the second one is not. I can see the call to the complete method in the bean but the button is not updated

Any advise?

Upvotes: 1

Views: 3440

Answers (2)

Nagesh14_8
Nagesh14_8

Reputation: 1

If you want to use disabled, do not use rendered property on command button.

<p:commandButton id="buttonId"  
actionListener="#{controller.function}"
value="#{controller.label}" 
disabled="#{controller.disableButton}" rendered="#{controller.renderButton}"  />

After removing rendered, button gets disabled without any issues.

Upvotes: 0

Fallup
Fallup

Reputation: 2427

Seems like you can't force update just with <p:ajax process="@this" update="genButton" />. I would try to update the button directly from the bean.

Since you are using primefaces you can use RequestContext; try to add this line to your complete method :

RequestContext.getCurrentInstance().addPartialUpdateTarget("genButton");

If you don't use prependId="false" on your wrapping form, you might need to specify the full path to the button e.g.:

RequestContext.getCurrentInstance().addPartialUpdateTarget("fooForm:genButton");

Upvotes: 2

Related Questions