Vaadin 7:TableFieldFactory [英] Vaadin 7 : TableFieldFactory

查看:29
本文介绍了Vaadin 7:TableFieldFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to implent TableFactory interface with specific fields related to propertyId ? I only get one type of field since i'm using a generic class for all my tables, and the i'm missing CheckBox boolean value (groovy code):

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" ).

So i need to use this class above which implments DefaultTableFieldfactory in order to have the null representation as "" (instead of "null" ) in my whole application.

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

The goal is to provide for my custom components (more than 30) this null representation in a single place, I want to use this class as my default factory for every table, and connect it like i've done before:

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 提供了一个 DefaultTableFieldFactory 来做映射

Vaadin provides a DefaultTableFieldFactory which does map

  • 日期到 DateField
  • 复选框的布尔值
  • TextField 以外的其他

DefaultTableFieldFactory 已在表上设置.因此,在您的情况下,如果您只想为布尔字段设置 CheckBoxes,我不会实现自己的 TableFieldFactory.举个例子:

The DefaultTableFieldFactory is already set on the table. So in your case, if you just want to have CheckBoxes for your boolean fields, I wouldn't implement an own TableFieldFactory. Here's an example:

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 建议:

If you really need to have your own TableFieldFactory then Vaadin recommends:

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

You could just implement the TableFieldFactory interface, but we recommend that you extend the DefaultFieldFactory according to your needs. In the default implementation, the mappings are defined in the createFieldByPropertyType() method (you might want to look at the source code) both for tables and forms.

在问题中给出的代码中,您总是返回一个 TextField.对于丢失的复选框,您需要在特定情况下返回一个复选框.

In your code given in the question you always return a TextField. For your missing CheckBoxes you need to return in the specific case a CheckBox.

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

Don't forget to setEditable(true) when using FieldFactories.

在 5.16.3 下此处的更多信息.编辑表中的值.

More information here under 5.16.3. Editing the Values in a Table.

这篇关于Vaadin 7:TableFieldFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆