GokcenG
GokcenG

Reputation: 2811

Different Custom Fields in Vaadin Table

I've a vaadin table backed by CrudBeanItemContainer:

CrudBeanItemContainer<MyBean> beanItemContainer = new CrudBeanItemContainer<MyBean>(
            MyBean.class, getTableFields());

for (MyBean bean : beanCollection) {
    beanItemContainer.addBean(bean);
}
table.setContainerDataSource(beanItemContainer);

MyBean has 3 fields:

public class ProcessInstanceVariable {  
private String name;
private Object value;
private VariableType variableType;
    ...
}

VariableType stores the type of value: Integer, Date, String, Boolean etc.

I'm listing MyBeans in the table. I want user to be able to edit the value on the table. For that, I need to add a field to Value column, it can be DateField, CheckBox etc. according to VariableType.

I've tried this:

Collection<?> itemIds = getTable().getItemIds();
for (Object itemId : itemIds) {
    Item item = getTable().getItem(itemId);
    Property value = item.getItemProperty("value");
    MyBean myBean = (MyBean) itemId;
    Property property = getField(myBean);
    boolean isRemoved = item.removeItemProperty("value");
    boolean isAdded = item.addItemProperty("value", property);
}

getField method returns the required field(CheckBox, DateField...). Although, isRemoved and isAdded is true, I don't see the field in the table.

How can I add the field w.r.t. the VariableType inside MyBean?

Upvotes: 0

Views: 3599

Answers (1)

Alex
Alex

Reputation: 591

Try generateCell():

table.addGeneratedColumn("ColumnName", new ColumnGenerator() {          
    @Override
    public Object generateCell(Table source, final Object itemId, Object columnId) {
        Class class = source.getItem(itemId).getItemProperty("variableType").getType();
        if (class == Boolean.class){
            CheckBox chk = new CheckBox(null);
            ...
            return chk;
        } else if (class == String.class){
            TextArea text = new TextArea(null);
            ...
            return text;
        }
    }
});

Upvotes: 3

Related Questions