I'm trying to find the equivalent of this XHTML code:
<h:selectBooleanCheckbox value="#{sandboxBean.selected}" >
<f:ajax listener="#{sandboxBean.handleToggle}" render="outputText" />
</h:selectBooleanCheckbox>
<br /><br />
<h:outputText value="#{sandboxBean.selected}" id="outputText"/>
for the case where the entire checkbox has to be dynamically created by the backing bean. I've managed to get some Ajax goodness with this code:
checkbox = new HtmlSelectBooleanCheckbox();
checkbox.setId(makeCheckboxId());
AjaxBehavior valueChangeAction = (AjaxBehavior)FacesContext.getCurrentInstance().getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
valueChangeAction.addAjaxBehaviorListener(new AjaxBehaviorListener() {
@Override
public void processAjaxBehavior(AjaxBehaviorEvent event) throws AbortProcessingException {
System.out.println("Ajax behavior called");
}
});
checkbox.addClientBehavior("valueChange", valueChangeAction);
but I can't figure out how to get the Ajax call to execute my handleToggle
method, nor how to give it easy access to the outputText
element that I want to render (in this cut-down example).
Also, this seems unexpectedly complicated: this all stems from a need for a table with an unknown number of columns. Am I likely just approaching this from fundamentally the wrong angle by creating the whole table in code?