set/unset checkboxes in JSF
Posted
by mykola
on Stack Overflow
See other posts from Stack Overflow
or by mykola
Published on 2010-05-27T07:58:37Z
Indexed on
2010/05/27
8:01 UTC
Read the original article
Hit count: 272
Hello, i've got one problem with checkboxes in JSF. I want them to behave dependently on each other, e.g., when i check a box which belongs to some object that has children then all checkboxes that belong to these children components must be checked either. And also when i uncheck one of child's checkbox the parent should be unchecked too. It's pretty simple with plain HTML/javascript, but i can't do anything with this under JSF. For some reason i can't set ID's for them because all checkboxes are rendered dynamically in a treetable and it prevents me from setting my own ID's, i.e. whatever i set in ID property only constant part will apply, all dynamic data that i pass is ignored. I tried to do it through valueChangeListener or validator but in both cases after i set needed values something sets them back! I don't know who does it and i can't do anything with this. Here's some code (i use OpenFaces treeTable):
<o:treeTable id="instTreeTable" var="inst" ...>
<...>
<o:column id="isGranted" width="10%">
<f:facet name="header">
<h:outputText value="#{msg.access_granted}" />
</f:facet>
<h:selectBooleanCheckbox
value="#{inst.assignedToUser}"
styleClass="treeTableText"
valueChangeListener="#{MbUserInstitutions.onAccessGrantedChanged}"
>
<a4j:support event="onchange" reRender="instTreeTable"/>
</h:selectBooleanCheckbox>
</o:column>
<...>
</o:treeTable>
MbUserInstitutions:
public void onAccessGrantedChanged(ValueChangeEvent event) {
Boolean granted = (Boolean) event.getNewValue();
Institution inst = getInstitution();
if (granted.booleanValue() && inst.hasChildren()) {
setChildrenInsts(inst);
} else if (!granted.booleanValue() && inst.getParentId() != null){
unsetParentInst(inst);
}
}
private Institution getInstitution() {
return (Institution) Faces.var("inst");
}
private void setChildrenInsts(Institution parent) {
for (Institution child: parent.getChildren()) {
child.setAssignedToUser(true);
if (child.hasChildren()) {
setChildrenInsts(child);
}
}
}
private void unsetParentInst(Institution child) {
child.setAssignedToUser(false);
for (Institution inst: coreInsts) {
if (inst.getId().equals(child.getParentId())) {
unsetParentInst(inst);
break;
}
}
}
© Stack Overflow or respective owner