Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
676 views
in Technique[技术] by (71.8m points)

vaadin7 - Vaadin 7 : TableFieldFactory

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
    }
}

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.

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())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Vaadin provides a DefaultTableFieldFactory which does map

  • Date to a DateField
  • Boolean to a CheckBox
  • other to TextField

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);

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

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.

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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...