使用BeanItemContainer添加表列

Ski*_*zzo 3 java vaadin vaadin7

我应该向具有BeanItemContainer数据源的表添加一列.

这是我的情况:

我有一个实体bean

@Entity
public class MyBean implements {

@Id
private Long id;

//other properties
Run Code Online (Sandbox Code Playgroud)

}

然后在我的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;

}
Run Code Online (Sandbox Code Playgroud)

现在,我想添加一个列,使我能够删除此容器中的项目.

我能怎么做?

nex*_*xus 6

您可以创建一个ColumnGenerator为您创建按钮.看看这里.

例:

假设我们有一个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;
    }

}
Run Code Online (Sandbox Code Playgroud)

然后,我们可以创建一个包含生成列的表,给出一个删除当前项的按钮.

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 ""
Run Code Online (Sandbox Code Playgroud)