Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Wednesday, 18 July 2012

Disabling button with JavaScript in ADF Faces

Disabling ADF Faces UI component on the client-side could be tricky sometimes. This post on Jobinesh's blog shows an example on how to disable af:inputText through javascript. You basically need to set unsecure="disabled" and clientComponent="true". But if you use button.setProperty('disabled', true) on a commandButton, your button will still look like undisabled (though it won't invoke action event), proper style class and 'disabled' attribute will not be applied to the button element.
To properly disable button you can use script like this:
setButtonDisabled = function (button, disabled) {
    if (button instanceof AdfRichCommandButton) {
        button.setProperty('disabled', disabled);
        AdfDomUtils.addOrRemoveCSSClassName(disabled,
            AdfRichUIPeer.getDomElementForComponent(button),
            AdfRichUIPeer.DISABLED_STYLECLASS);
        var buttonDom = button.getPeer().getButtonElement(button);
        if (disabled) {
            buttonDom.setAttribute('disabled', 'disabled');
        } else {
            buttonDom.removeAttribute('disabled');
        }       
    }
}
After setting 'disabled' property to 'true', I'm adding/removing proper style class and adding/removing 'disabled' html attribute on <button> element.