在 ajax 更新时删除了检票口表单更改



我有一个非常大而复杂的表单,其中包含许多表单组件。

在其中一个下拉字段中,我添加了一个AjaxFormComponentUpdatingBehavior来处理下拉列表中的更改。基于这些更改,我正在以这种方式更新表单中的其他一些字段:

masterFooDropDown.add(new AjaxFormComponentUpdatingBehavior("change") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
String value = (String) getFormComponent().getModelObject();
if (value != null) {
for (Foo foo: form.getModelObject().getFoos()) {
foo.setValue(value);
}
target.add(form);
}
}
});

Foo字段使用下拉列表中的值正确更新。

问题是表单中其他文本字段中的任何更改都将被删除。我知道会发生这种情况,因为它们尚未在模型中更新。

我该如何解决这个问题? 我可以以某种方式将表单中到目前为止填写的所有数据写入模型(无需提交表单(吗?

我需要将整个表单添加到 ajax 方法中的目标,因为应该更新的字段是表单模型对象的子项,并动态添加到表单中。例如,我不能做target.add(fooFieldX)因为可能有任意数量的"fooFields"。

  1. 您可以在所有其他窗体(选择(组件上使用 AjaxFormComponentUpdatingBehavior/AjaxFormChoiceComponentUpdatingBehavior,以便它们在修改时更新窗体的模型对象。

  2. 您可以使用 Component#visitChildren(( 并动态决定将哪些特定的表单组件添加到 AjaxRequestTarget 中,如果这很容易决定的话。甚至 AjaxRequestTarget 也有方法#addChildren()但过滤子组件并不灵活。

最新更新