我应该向具有 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;
}
现在,我想添加一个列,使我能够删除此容器中的项目。
我该怎么办?
最佳答案
您可以创建一个 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 ""
关于java - 使用 BeanItemContainer 添加表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21087386/