如何清除FieldGroup
7应用中Vaadin中的字段?
当我致电FieldGroup::setItemDataSource
并传递null
时,我会收到文字" null"出现在由各种数据类型(Number,java.util.Date)支持的TextField
对象中。我可以理解空值显示" null"作为字符串表示。但为什么那些" null"字符串默认不显示?默认情况下,我在所有字段中都看到空的TextField字符串。
例如,请考虑下面的示例应用。在以下屏幕截图中我们:
FieldGroup::setItemDataSource( null )
,传递null。这是Java 8中完整的单文件Vaadin 7.5.3应用程序(使用Lambda语法)。
package com.example.gridexample;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.util.PropertysetItem;
import com.vaadin.data.util.converter.StringToDateConverter;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
/**
*
*/
@Theme ( "mytheme" )
@Widgetset ( "com.example.gridexample.MyAppWidgetset" )
public class MyUI extends UI
{
PropertysetItem itemAaron = null;
PropertysetItem itemBernard = null;
TextField nameField = null;
TextField ageField = null;
TextField dateOfBirthField = null;
FieldGroup fieldGroup = null;
ZoneId zone = ZoneId.systemDefault(); // Or ZoneId.of( "America/Montreal" ) ;
@Override
protected void init ( VaadinRequest vaadinRequest ) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin( true );
layout.setSpacing( true );
setContent( layout );
Button buttonAaron = new Button( "Show Aaron" );
buttonAaron.addClickListener( ( ClickEvent event ) -> {
showAaron();
} );
layout.addComponent( buttonAaron );
Button buttonBernard = new Button( "Show Bernard" );
buttonBernard.addClickListener( ( ClickEvent event ) -> {
showBernard();
} );
layout.addComponent( buttonBernard );
Button buttonNoOne = new Button( "Show No One" );
buttonNoOne.addClickListener( ( ClickEvent event ) -> {
showNoOne();
} );
layout.addComponent( buttonNoOne );
this.itemAaron = new PropertysetItem();
this.itemAaron.addItemProperty( "name" , new ObjectProperty<>( "Aaron" ) );
this.itemAaron.addItemProperty( "age" , new ObjectProperty<>( 42 ) );
this.itemAaron.addItemProperty( "dateOfBirth" , new ObjectProperty<>( Date.from( ZonedDateTime.now( this.zone ).minusYears( 42 ).toInstant() ) ) );
this.itemBernard = new PropertysetItem();
this.itemBernard.addItemProperty( "name" , new ObjectProperty<>( "Bernard" ) );
this.itemBernard.addItemProperty( "age" , new ObjectProperty<>( 24 ) );
this.itemBernard.addItemProperty( "dateOfBirth" , new ObjectProperty<>( Date.from( ZonedDateTime.now( this.zone ).minusYears( 24 ).toInstant() ) ) );
this.nameField = new TextField( "Name:" );
this.ageField = new TextField( "Age:" );
this.dateOfBirthField = new TextField( "Date Of Birth:" );
// Customize the Converter assigned to our date-time field. A StringToDateConverter object is assigned by default.
//this.dateOfBirthField.setConverter( new StringToDateConverter() ); // Actually the default.
// this.dateOfBirthField.setConverter( new SpecialStringToDateConverter() );
layout.addComponent( this.nameField );
layout.addComponent( this.ageField );
layout.addComponent( this.dateOfBirthField );
this.fieldGroup = new FieldGroup();
this.fieldGroup.bind( this.nameField , "name" );
this.fieldGroup.bind( this.ageField , "age" );
this.fieldGroup.bind( this.dateOfBirthField , "dateOfBirth" );
this.fieldGroup.setReadOnly( true );
}
private void showAaron () {
this.fieldGroup.setItemDataSource( this.itemAaron );
}
private void showBernard () {
this.fieldGroup.setItemDataSource( this.itemBernard );
}
private void showNoOne () {
this.fieldGroup.setItemDataSource( null );
}
@WebServlet ( urlPatterns = "/*" , name = "MyUIServlet" , asyncSupported = true )
@VaadinServletConfiguration ( ui = MyUI.class , productionMode = false )
public static class MyUIServlet extends VaadinServlet
{
}
}
答案 0 :(得分:1)
在点击任何按钮(初始状态)之前,执行这些行:
TextField nameField = null;
//...
this.nameField = new TextField( "Name:" );
文档说:
public TextField(java.lang.String caption)
构造一个带有给定标题的空 TextField。
因此,应用程序中的所有文本字段都为空值。但是......在那之后你使用:
this.fieldGroup.bind( this.nameField , "name" );
该值是否应为null?同样,[bind()
](https://vaadin.com/api/com/vaadin/data/fieldgroup/FieldGroup.html#bind(com.vaadin.ui.Field,java.lang.Object)的文档说:
使用当前项目中的给定propertyId绑定字段。如果某个项目未设置,则绑定延期,直到使用
setItemDataSource(Item)
设置该项目。
因此,您必须使用此方法查看bind
效果。
您问:但为什么默认情况下不显示这些“空”字符串?
我希望我的解释清楚,最初每个TextField都有空值。调用bind()
函数后,它仅在setItemDataSource(Item)
之后影响视图,但是稍后在按钮的clickListener中调用此函数。所以最初很好,所有字段都有空值。
答案 1 :(得分:0)
尝试添加this.nameField.setNullRepresentation("");
并在每个字段上调用相同的方法,根据Book of Vaadin它应该可以解决您的问题。