我应该在具有BeanItemContainer数据源的表中添加一列。
这是我的情况:
我有一个实体bean
@Entity
public class MyBean implements {
@Id
private Long id;
//other properties
}
然后在我的vaadin面板中我有这个方法
private Table makeTable(){
Table table = new Table();
tableContainer = new BeanItemContainer<MyBean>(MyBean.class);
table.setContainerDataSource(tableContainer);
table.setHeight("100px");
table.setSelectable(true);
return table;
}
现在,我想添加一个列,使我能够删除此容器中的项目。
我该怎么办?
答案 0 :(得分:6)
您可以创建ColumnGenerator
,为您创建按钮。
看看here。
示例:
假设我们有一个MyBean类:
public class MyBean {
private String sDesignation;
private int iValue;
public MyBean() {
}
public MyBean(String sDesignation, int iValue) {
this.sDesignation = sDesignation;
this.iValue = iValue;
}
public String getDesignation() {
return sDesignation;
}
public int getValue() {
return iValue;
}
}
然后我们可以使用生成的列创建一个表,给出一个删除当前项的按钮。
Table table = new Table();
BeanItemContainer<MyBean> itemContainer = new BeanItemContainer<MyBean>(MyBean.class);
table.setContainerDataSource(itemContainer);
table.addItem(new MyBean("A", 1));
table.addItem(new MyBean("B", 2));
table.addGeneratedColumn("Action", new ColumnGenerator() { // or instead of "Action" you can add ""
@Override
public Object generateCell(final Table source, final Object itemId, Object columnId) {
Button btn = new Button("Delete");
btn.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
source.removeItem(itemId);
}
});
return btn;
}
});
table.setVisibleColumns(new Object[]{"designation", "value", "Action"}); // if you added "" instead of "Action" replace it by ""
答案 1 :(得分:0)
我建议改用shourtcut:
table.addShortcutListener(new ShortcutListener("Delete", KeyCode.DELETE, null) {
@Override
public void handleAction(final Object sender,
final Object target) {
if (table.getValue() != null) {
// here send event to your presenter to remove it physically in database
// and then refresh the table
// or just call tableContainer.removeItem(itemId)
}
}
});
如果你不想要shourtcuts,你需要添加列,例如:
table.addContainerProperty("Delete", Button.class, null);
然后放在那里做同样动作的按钮。