During the writing of 'Learning Vaadin', I had many themes I wanted to write about: components data, SQL container filtering, component alignment and expand ration, separation of concerns between graphic designers and developers, only to name a few. Unfortunately, books are finite in space as well as in time and I was forced to leave out some interesting areas of Vaadin that couldn’t fit in, much to my chagrin.
Give the success of 'Learning Vaadin', I’ve decided to create a site that is meant to gap the bridge between what I wanted and what I could: More Vaadin. Expect to see there articles related to Vaadin! I’ve also decided to open the platform to other contributors, for those interested.
As a bonus, this is the first article (original article here).
Table generated buttons
Before diving in the middle of the subject, let’s have a use-case first. Imagine we have a CRUD application and our current screen lists the datastore entities in a table, a line per entity and a column per property.
Now, in order to implement the Delete functionality, we use the addGeneratedColumn
method of the Table
component to display a "Delete" button, like in the following screenshot:
The problem lies in the action that has to take place when the button is pressed. The behavior needs to be determined when the page is generated, so that the event is processed directly on the server-side: in other words, we need a way to pass the entity identifier (or the container’s item or even the container’s item id) to the button somehow.
The solution is very simple to implement, with just the use of the final
keyword, to let nested anonymous class methods access parameters:
table.addGeneratedColumn("", new ColumnGenerator() {
@Override
public Object generateCell(final Table source, final Object itemId, Object columnId) {
Button button = new Button("Delete");
button.addListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
source.getContainerDataSource().removeItem(itemId);
}
});
return button;
}
});
Remember to regularly check More Vaadin for other reindeer articles 🙂