单击添加按钮后,将创建带有文本框和复选框的空行。我必须在文本框中输入详细信息,但仅应选中一个复选框。现在,我可以选中所有复选框。但是,如果选中了一个,则其他复选框也应取消选中。
<p:commandButton value="Add"
actionListener="#{createReturns.addReturnBodyToList}" ajax="true"
update="bodyTable messages" style="margin-bottom:10px;"/>
<p:dataTable id="bodyTable" value="#{createReturns.returnBodyList}"
var="returnBody">
<p:column headerText="Element Name" style="width:250px;">
<p:inputText id="elementName" value="#{returnBody.elementName}"
style="width:240px;" />
</p:column>
<p:column headerText="KeyColumn" style="width:80px">
<p:selectBooleanCheckbox value="#{returnBody.keyColumn}" id="key">
<p:ajax listener="#{createReturns.toggleCheckBox()}"
event="click" update="key">
<f:attribute name="element" value="#{returnBody.elementName}"></f:attribute>
</p:ajax>
</p:selectBooleanCheckbox>
</p:column>
</p:datatable>
用于按钮的actionlistener,用于在数据表中添加空行
public void addReturnBodyToList() {
returnBody = new ReturnBody();
returnBodyList.add(returnBody);
}
ajax侦听器以取消选中bean中的其他复选框
public void toggleCheckBox(ActionEvent actionEvent) {
String elementName = (String) actionEvent.getComponent()
.getAttributes().get("element");
for (ReturnBody body : returnBodyList) {
if (!body.getElementName().equals(elementName))
body.setKeyColumn(false);
System.out.println("Check ajax: " + body.toString());
}
}
您可以在toggleCheckBox()方法中实现。
boolean flag; //Global
if(!flag)
setFlag(true);
lastChecked
对于每个检查,检查标志,并将最近的检查存储在变量中。将先前检查的ReturnBody
对象键列设置为false,作为body.setKeyColumn(false)