我有一个由CrudBeanItemContainer支持的vaadin表:
CrudBeanItemContainer<MyBean> beanItemContainer = new CrudBeanItemContainer<MyBean>(
MyBean.class, getTableFields());
for (MyBean bean : beanCollection) {
beanItemContainer.addBean(bean);
}
table.setContainerDataSource(beanItemContainer);
MyBean有3个字段:
public class ProcessInstanceVariable {
private String name;
private Object value;
private VariableType variableType;
...
}
VariableType存储值的类型:Integer,Date,String,Boolean等。
我在表格中列出了MyBeans。我希望用户能够编辑表格上的值。为此,我需要在 Value 列中添加一个字段,根据VariableType可以是DateField,CheckBox等。
我试过这个:
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方法返回必填字段(CheckBox,DateField ...)。 虽然 isRemoved 且 isAdded 为true,但我没有在表格中看到该字段。
如何添加字段w.r.t. MyBean中的VariableType?
答案 0 :(得分:3)
尝试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;
}
}
});