gpt4 book ai didi

data-binding - Vaadin 8 Binder 中相关计算属性的自动更新显示

转载 作者:行者123 更新时间:2023-12-05 03:09:24 25 4
gpt4 key购买 nike

当在 Vaadin 8 中使用 Binder 和一个具有只读计算属性(其值来自另一个属性)的 bean 时,如何让 TextField 自动更新驱动属性值改变时派生计算结果的显示?

在下面的示例中,当用户更改“出生年份”字段时,如何让“年龄”字段更新其计算?

enter image description here

Vaadin 8 的完整工作示例。

package com.example.val;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.Binder;
import com.vaadin.data.converter.StringToIntegerConverter;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;

import javax.servlet.annotation.WebServlet;
import java.time.LocalDate;
import java.time.ZoneId;

/**
* This UI is the application entry point. A UI may either represent a browser window
* (or tab) or some part of a html page where a Vaadin application is embedded.
* <p>
* The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
* overridden to add component to the user interface and initialize non-component functionality.
*/
@Theme ( "mytheme" )
public class MyUI extends UI {

Person person;
Binder < Person > binder;

@Override
protected void init ( VaadinRequest vaadinRequest ) {
// Data model
this.person = new Person ( "Jean-Luc", 1955 );

// Widgets
final TextField nameField = new TextField ( "Type the person’s name here:" );
final TextField yearOfBirthField = new TextField ( "Type the year of birth here:" );
final TextField ageField = new TextField ( "Approximate age:" );
ageField.setReadOnly ( true );
final Label beanToString = new Label ( );

// Binder
this.binder = new Binder <> ( );
binder.forField ( nameField )
.bind ( Person:: getName, Person:: setName );
binder.forField ( yearOfBirthField )
.withConverter ( new StringToIntegerConverter ( "Input must be Integer" ) )
.bind ( Person:: getYearOfBirth, Person:: setYearOfBirth );
binder.forField ( ageField )
.withConverter ( new StringToIntegerConverter ( "" ) )
.bind ( Person:: getAge, null );
binder.setBean ( this.person );

final Button button = new Button ( "Save" );
button.addClickListener ( event -> {
if ( binder.validate ( ).isOk ( ) ) {
// With `setBear`, the Person object is always up-to-date as long as there are no validation errors.
// MyBackend.updatePersonInDatabase(person);
beanToString.setValue ( this.person.toString ( ) );
} else { // Else bean flunks validation.
beanToString.setValue ( "The Person bean has invalid state." );
}
} );


this.setContent ( new VerticalLayout ( nameField, yearOfBirthField, ageField, button, beanToString ) );
}

@WebServlet ( urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true )
@VaadinServletConfiguration ( ui = MyUI.class, productionMode = false )
public static class MyUIServlet extends VaadinServlet {
}
}

class Person {
private Integer yearOfBirth;
private String name;


public Person ( String name_, Integer yearOfBirth_ ) {
this.name = name_;
this.yearOfBirth = yearOfBirth_;
}

public String getName ( ) {
return name;
}

public void setName ( String name ) {
this.name = name;
}

public Integer getYearOfBirth ( ) {
return yearOfBirth;
}

public void setYearOfBirth ( Integer yearOfBirth ) {
this.yearOfBirth = yearOfBirth;
}

// Read-only property 'age', calculated rather than stored.
public Integer getAge ( ) {
LocalDate today = LocalDate.now ( ZoneId.systemDefault ( ) );
Integer years = ( today.getYear ( ) - this.yearOfBirth );
return years;
}

@Override
public String toString ( ) {
return "Person{ " +
"yearOfBirth=" + yearOfBirth +
", age='" + this.getAge ( ) + "'" +
", name='" + name + "'" +
" }";
}
}

最佳答案

我发现最简单的方法是修改 yearOfBirthField 的绑定(bind)代码,如下所示:

binder.forField(yearOfBirthField)
.withConverter(new StringToIntegerConverter("Input must be Integer"))
.bind(Person::getYearOfBirth, (Setter<Person, Integer>) (person1, integer) -> {
person1.setYearOfBirth(integer);
ageField.setValue(person1.getAge().toString());
});

这有效地将 yearOfBirthField 绑定(bind)到 yearOfBirth 属性并更新了 person bean 的 age 属性。以任何方式更改 person bean 的成员,例如通过调用 person.setYearOfBirth(1977) 不会立即对字段产生影响。 Vaadin 的更新机制只能在一个方向上工作。字段修改会转移到 bean,但反之则不然。

编辑

在 Vaadin 8 中,没有内置方法可以在 bean 属性更改时自动更新 Field。当 yearOfBirthField 的值更改时,新值将传播到 person bean,并且该人的 yearOfBirth 成员会通过绑定(bind)自动更改。但是之后 Vaadin 不会获取 bean 的所有绑定(bind)字段的当前值。所以 ageField 没有更新,也没有反射(reflect)当前值。

因此,要使 ageField 显示更新后的值,您必须以编程方式更新 ageField。您可以在绑定(bind)器上再次设置整个 bean,这会导致调用所有 getter,但最简单的方法是在设置 yearOfBirth 之后设置 ageField 的值。这是在 setter 绑定(bind)的建议修改中完成的。

关于data-binding - Vaadin 8 Binder 中相关计算属性的自动更新显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43026546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com