我有一个带有添加和删除按钮的动态可扩展表:
<h:form>
<h:dataTable id="tblFields" value="#{bean.fields}" var="field">
<h:column>
<h:inputText value="#{field.value}" />
</h:column>
<h:column>
<h:inputText value="#{field.value2}" />
</h:column>
<h:column>
<h:inputText value="#{field.value3}" />
</h:column>
<h:column>
<h:commandButton value="Remove">
<f:ajax listener="#{bean.onButtonRemoveFieldClick(field)}" immediate="true" render="@form" />
</h:commandButton>
</h:column>
</h:dataTable>
<h:commandButton value="Add">
<f:ajax listener="#{bean.onButtonAddFieldClick}" execute="@form" render="tblFields" />
</h:commandButton>
</h:form>
这是关联的支持 bean:
@Named
@ViewScoped
public class Bean {
private List<Field> fields;
@PostConstruct
public void init() {
fields = new ArrayList();
fields.add(new Field());
}
public List<Field> getFields() {
return fields;
}
public void setFields(List<Field> fields) {
this.fields = fields;
}
public void onButtonRemoveFieldClick(Field field) {
fields.remove(field);
}
public void onButtonAddFieldClick() {
fields.add(new Field());
}
}
用例如下:
- 多次按下添加按钮。
- 填写所有值。
- 按一个随机行的删除按钮。
之后,到目前为止填充的所有值都会消失并显示为空白。按下删除按钮后如何保持它们充满?