gridPro 中自定义日期选择器组件的值始终为空
Posted
技术标签:
【中文标题】gridPro 中自定义日期选择器组件的值始终为空【英文标题】:Value of custom Date Picker Component in gridPro is always null 【发布时间】:2022-01-04 16:29:56 【问题描述】:我想在 vaadin gridPro 中使用 DatePicker 作为自定义组件。 由于只有复选框、选择和文本作为 EditColumn 原生支持,我尝试使用自定义。
final DatePicker component = new DatePicker();
this.medicationAdjustmentRules.addEditColumn(value -> (value.getValidUntil() != null ? value.getValidUntil().format(this.formatter) : null))
.custom(component, (medicationAdjustmentRule, newValue) -> medicationAdjustmentRule.setValidUntil(component.getValue() != null ? component.getValue() : newValue))
.setHeader("validUntil");
component.getValue() 和 newValue 始终为 null。
我做错了什么?
【问题讨论】:
我以前没有尝试过你在做什么,很难全面了解你在做什么。也许您可以尝试为 DatePicker 预先设置一个值,以验证它是否仍然为空? 【参考方案1】:我似乎能够从组件实例和 ItemUpdater 参数中获取一个值。这是一个完整的例子:
@Route(value = "gridpro", layout = MainLayout.class)
public class GridProView extends VerticalLayout
public static class Person
private String firstName;
private String lastName;
private LocalDate birthDate;
public String getFirstName()
return firstName;
public void setFirstName(String firstName)
this.firstName = firstName;
public String getLastName()
return lastName;
public void setLastName(String lastName)
this.lastName = lastName;
public LocalDate getBirthDate()
return birthDate;
public void setBirthDate(LocalDate birthDate)
this.birthDate = birthDate;
public Person(String firstName, String lastName, LocalDate birthDate)
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
public List<Person> getData()
List<Person> persons = new ArrayList<>();
persons.add(new Person("Zach", "Quinto", LocalDate.of(2000, 5, 2)));
persons.add(new Person("Paula", "Boulon", LocalDate.of(1982, 11, 24)));
persons.add(new Person("Foo", "Bar", LocalDate.of(2020, 01, 01)));
return persons;
public GridProView()
GridPro<Person> gridPro = new GridPro<>();
gridPro.addColumn(Person::getFirstName).setHeader("First name");
gridPro.addColumn(Person::getLastName).setHeader("Last name");
DatePicker component = new DatePicker();
gridPro.addEditColumn(Person::getBirthDate).custom(component,
(ItemUpdater<Person, LocalDate>) (person, newValue) ->
System.out.println("Value in component: " + component.getValue());
System.out.println("newValue: " + newValue);
person.setBirthDate(newValue);
);
gridPro.setItems(getData());
add(gridPro);
这会输出如下内容:
Value in component: 2000-07-18
newValue: 2000-07-18
Value in component: 1981-11-24
newValue: 1981-11-24
Value in component: 2020-01-08
newValue: 2020-01-08
【讨论】:
以上是关于gridPro 中自定义日期选择器组件的值始终为空的主要内容,如果未能解决你的问题,请参考以下文章