The ajax listener is not called for a checkbox that is in a datatable. I reduced the problematic code to the following simple (and not very meaningful) example.
I have a datatable and in every row a checkbox. By clicking the checkbox the ajax listener shall be called but it isn't called. I have also a checkbox outside the datatable. When clicking this checkbox the ajax listener is called. What is wrong with the checkboxes in the datatable or what should I do to get it working?
Here is the xhtml file (I'm using tomahawk):
<h:form>
<h:selectBooleanCheckbox id="selectAssignmentOutside" value="#{myController.assignments[100000]}">
<f:ajax render="selectionStateOutside" listener="#{myController.processAjaxBehavior}"/>
</h:selectBooleanCheckbox>
<h:outputText id="selectionStateOutside" value="#{myController.assignments[100000] ? 'selected' : 'not selected'}"/>
<p/>
<t:dataTable id="assignmentsTable" value="#{myController.allEntities}" var="row" forceIdIndexFormula="#{row.id}" preserveDataModel="false">
<h:column>
<h:outputText value="#{row.id}"/>
</h:column>
<h:column>
<h:selectBooleanCheckbox id="selectAssignment" value="#{myController.assignments[row.id]}">
<f:ajax render="selectionState" listener="#{myController.processAjaxBehavior}"/>
</h:selectBooleanCheckbox>
<h:outputText id="selectionState" value="#{myController.assignments[row.id] ? 'selected' : 'not selected'}"/>
</h:column>
</t:dataTable>
</h:form>
the controller:
@ManagedBean
@ViewScoped
public class MyController
{
private List<Entity> entities;
private Map<Long, Boolean> assignments;
public Map<Long, Boolean> getAssignments() {
if (assignments == null) {
assignments = new HashMap<>();
assignments.put( 100000L, true );
assignments.put( 100001L, true );
assignments.put( 100002L, false );
assignments.put( 100003L, false );
}
return assignments;
}
public List<Entity> getAllEntities() {
entities = new ArrayList<>();
entities.add( new Entity( 100000L ));
entities.add( new Entity( 100001L ) );
entities.add( new Entity( 100002L ) );
entities.add( new Entity( 100003L ) );
return entities;
}
public void processAjaxBehavior(AjaxBehaviorEvent event)
{
System.out.println("#### processAjaxBehavior");
// here some things should be done in model
// and the component re-rendered by ajax component shows the result
}
}
Tomahawk's datatable has a bug in generating the client id if the attribute "forceIdIndexFormula" is set. After removing the attribute "forceIdIndexFormula" from "t:datatable" it works as expected.