Skizzo
Skizzo

Reputation: 2983

Add table column with BeanItemContainer

I should add a column to a table that has a BeanItemContainer datasource.

This is my situation:

I hava an entity bean

@Entity
public class MyBean implements {

@Id
private Long id;

//other properties

}

Then in my vaadin panel i have this method

private Table makeTable(){

    Table table = new Table();
    tableContainer = new BeanItemContainer<MyBean>(MyBean.class);
    table.setContainerDataSource(tableContainer);

    table.setHeight("100px");
    table.setSelectable(true);
    return table;

}

Now, I want to add a column that should give me the ability to delete an item in this container.

How can i do?

Upvotes: 3

Views: 5238

Answers (2)

nexus
nexus

Reputation: 2937

You could create a ColumnGenerator which creates the button for you. Have a look here.

Example:

Let's say we have a MyBean class:

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;
    }

}

We then can create a table with a generated column giving a button to delete the current item.

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 ""

Upvotes: 6

dzezzz
dzezzz

Reputation: 1005

I would recommend using shourtcut instead:

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)
            }
        }
    });

if you don't want shourtcuts you would need add the column, eg:

table.addContainerProperty("Delete", Button.class, null);

and then put there the button that would do the same action.

Upvotes: 0

Related Questions