【问题标题】:Vaadin 7 : TableFieldFactoryVaadin 7:表格字段工厂
【发布时间】:2014-01-09 20:54:46
【问题描述】:

有没有办法使用与 propertyId 相关的特定字段来实现 TableFactory 接口? 我只得到一种类型的字段,因为我对所有表都使用了一个通用类,并且我缺少 CheckBox 布尔值(groovy 代码):

class DefaultTableFieldFactory implements TableFieldFactory {
    @Override
    public Field<?> createField(Container container, Object itemId, Object propertyId, Component component) {
        TextField t = new TextField()

        switch(propertyId) {
            case "firstname": t.setNullRepresentation("");
            case "lastname": t.setNullRepresentation("");
            case "mobile": t.setNullRepresentation("");
            case "tel": t.setNullRepresentation("");
            case "email": t.setNullRepresentation("");
            default: break;
        }
        t.setWidth("95px")

        return t
    }
}

所以我需要使用上面实现 DefaultTableFieldfactory 的此类,以便在我的整个应用程序中将 null 表示为 "" (而不是 "null" )。

目标是在一个地方为我的自定义组件(超过 30 个)提供这个 null 表示,我想使用这个类作为每个表的默认工厂,并像以前一样连接它:

def contacts = (Grails.get(FundService)).getAllContacts(fundId)
        def cContainer = new BeanItemContainer<Contact>(Contact.class,contacts)


        def t = new Table()
        t.containerDataSource = cContainer
        t.setTableFieldFactory(new DefaultTableFieldFactory())

【问题讨论】:

    标签: vaadin vaadin7


    【解决方案1】:

    Vaadin 提供了一个可以映射的 DefaultTableFieldFactory

    • 日期字段
    • 复选框的布尔值
    • TextField 的其他内容

    DefaultTableFieldFactory 已在表上设置。因此,在您的情况下,如果您只想为布尔字段使用 CheckBox,我不会实现自己的 TableFieldFactory。这是一个例子:

    Table table = new Table();
    
    table.addContainerProperty("text", String.class, "");
    table.addContainerProperty("boolean", Boolean.class, false);
    table.setEditable(true);
    
    Object itemId = table.addItem();
    table.getItem(itemId).getItemProperty("text").setValue("has accepted");
    table.getItem(itemId).getItemProperty("boolean").setValue(true);
    

    如果您真的需要拥有自己的 TableFieldFactory,那么 Vaadin 建议:

    您可以只实现 TableFieldFactory 接口,但我们 建议您根据您的扩​​展 DefaultFieldFactory 需要。在默认实现中,映射定义在 createFieldByPropertyType() 方法(您可能想查看 源代码)用于表格和表格。

    在问题中给出的代码中,您总是返回一个 TextField。对于缺少的 CheckBox,您需要在特定情况下返回 CheckBox。

    使用 FieldFactories 时不要忘记setEditable(true)

    更多信息here 5.16.3 下。编辑表中的值。

    【讨论】:

    • 谢谢,但我已经知道什么是 DefaultTableFactory,因为我正在我的代码中实现它。这里的目标是提供这个工厂来处理单个工厂中的不同类型的字段,并将这种模式应用于表的容器数据源(参见上面修改的代码)
    • @ludo_rj 然后看看 Vaadin 提供的 DefaultFieldFactory 类中的 createFieldByType() 方法,它在适当的时候返回一个 CheckBox。
    • 是的,这就是我要找的那个:) 因为 DefaultFieldFactory.createFieldByPropertyType 是静态的,所以它不能被覆盖,所以它需要在 TableFieldFactory 中正确实现额外代码..
    • @ludo_rj 我已经在我的回答中提到过,如果你仔细看看的话。
    猜你喜欢
    • 2013-01-13
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多