我有一个组件(例如TextField)将根据其他选择显示,例如单选按钮,当它被隐藏时,我不希望应用此字段上的绑定,所以
在某些地方:
Binder binder = new Binder<SomeDTO>(SomeDTO.class);
TextField conditionalComponet = TextField("A conditional component: ");
binder.bind(conditionalComponet, "propertyX");
在其他地方:
SomeDTO someDTO = new SomeDTO;
binder.writeBean(someDTO); //Here propertyX shouldn't be filled, i.e. bind should
//not be applied, to propertyX of SomeDTO if the
//conditionalComponet is hidden.
我不想从布局中删除组件,因为将它放在相同的位置将是一个问题。我尝试setVisible(false)
,setEnabled(false)
和setReadOnly(true)
,但没有人阻止应用绑定。有一种简单的方法吗?
我正在使用Vaadin 8.
答案 0 :(得分:2)
@Morfic答案不再正确。 Vaadin 8现在支持取消绑定字段:
TextField usernameField;
binder.forField(this.usernameField)
.bind(User::getUsername, User::setUsername);
binder.removeBinding(this.usernameField);
答案 1 :(得分:1)
据我所知,没有直接方法可以防止绑定时将值设置为字段。尽管如此,通过使用绑定(HasValue&lt; FIELDVALUE&gt;字段,ValueProvider<BEAN,FIELDVALUE> getter,Setter<BEAN,FIELDVALUE> setter)限制(?!) >方法变种。
有关示例,您可以查看以下代码。请注意,它只是显示效果的基本部分,因此它没有所有的功能,例如禁用或重置使用日期字段和复选框:
import com.vaadin.data.Binder;
import com.vaadin.data.ValidationException;
import com.vaadin.ui.*;
import java.io.Serializable;
import java.time.LocalDate;
public class DisableBindingForField extends VerticalLayout {
private final Person person = new Person("Dark Vaper");
private final TextField name = new TextField("Name:");
private final CheckBox isEmployed = new CheckBox("Is employed:");
private final DateField dateOfEmployment = new DateField("Date of employment:");
private final Binder<Person> binder = new Binder<>(Person.class);
private final Button button = new Button("Save");
public DisableBindingForField() {
// manually bind the custom field separately (https://vaadin.com/docs/-/part/framework/datamodel/datamodel-forms.html - scroll to the end)
binder.bind(dateOfEmployment, person -> person.dateOfEmployment, (person, dateOfEmployment) ->
// if the check-box is checked then populate the pojo with the value, otherwise reset the pojo value to null
person.setDateOfEmployment((isEmployed.getValue()) ? dateOfEmployment : null)
);
// automatically bind the rest of the fields
binder.bindInstanceFields(this);
// initial reading of the bean
binder.readBean(person);
// add components to the user interface
addComponents(name, isEmployed, dateOfEmployment, button);
// simulate a "save button"
button.addClickListener(event -> {
try {
binder.writeBean(person);
Notification.show(person.toString());
} catch (ValidationException e) {
e.printStackTrace();
}
});
}
// basic pojo for binding
public class Person implements Serializable {
private String name;
private Boolean isEmployed;
private LocalDate dateOfEmployment;
public Person(final String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public boolean isEmployed() {
return isEmployed;
}
public void setEmployed(boolean employed) {
isEmployed = employed;
}
public LocalDate getDateOfEmployment() {
return dateOfEmployment;
}
public void setDateOfEmployment(LocalDate dateOfEmployment) {
this.dateOfEmployment = dateOfEmployment;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", isEmployed=" + isEmployed +
", dateOfEmployment=" + dateOfEmployment +
'}';
}
}
}
结果:
P.S。:替代流利制作者风格:
binder.forField(dateOfEmployment)
.bind(person -> person.dateOfEmployment, (person, dateOfEmployment) ->
// if the check-box is checked then populate the pojo with the value, otherwise reset the pojo value to null
person.setDateOfEmployment((isEmployed.getValue()) ? dateOfEmployment : null)
);