开发者

Vaadin table hide columns and container customization

I am testing a project, using Vaadin and Hibernate. I am trying to use the HbnContainer class to show data into table. The problem is that I do not want to show all the properties of the two classes in the table.

For example:

@Entity
@Table(name="users")
class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;

@ManyToOne(cascade=CascadeType.PERSIST)
private UserRole role;

//getters and setters
}

and a second class:

@Entity
@Table(name="user_roles")
class UserRole {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;

//getters and setters
}

Next, I retrieve my data using the HbnContainer, and connect it to the table:

HbnContainer container = new HbnContainer(User.class, app);
table.setContainerDataSource(container);

The Table will only display the columns from User, and for the "role" it will put the role id instead. How can I hide that column, and replace it with the UserRole.name ?

I managed to use a ColumnGenerator() to get the string value in the table, for the UserRole - but I couldn't remove the previous column, with the numerical value.

What am I missing? Or, what is the best way to "customize" your data, before displaying a table (if i want to show data in a table from more than one object type.. what do I do?)

If I can't find a simple solution soon, I think I will just build the tables "by hand"..

So, any advice on this matter?

EDIT:

I did not express myself to well before. What I need to know is how to use nested pojos, with HbnContainer, and control what properties (and "sub-properties") appear in the table. I tried to Extend and reimplement some parts of HbnContainer, but.. couldn't do it properly.

For the previous example, the table generated from the Users table looks like:

Name  |Role
George| 1
Alex  | 2 

I want something like:

Name  | Role
G开发者_StackOverflow中文版eorge| admin
Alex  | user


What you want to do is to define which properties should be visible in the table (thus excluding the role id). This can be achieved with the setVisibleColumns() method.


Instead of hiding column / adding generated column you can override formatPropertyValue method in Table class.

final Table table = new Table() {
    @Override
    protected String formatPropertyValue(Object rowId, Object colId, Property property) {
        if ("column".equals(colId)) {
            return "something";
        }

        return super.formatPropertyValue(rowId, colId, property);
    }
};

To get the POJO (in your case User object) you can use this construction:

User blogger = (User) ((HbnContainer.EntityItem.EntityItemProperty) property).getPojo();


I had the same problem as you, this helped (hope it helps you too)

tblUsers.addGeneratedColumn("userRole", new Table.ColumnGenerator() {
            public Object generateCell(final Table source, final Object itemId, final Object columnId) {
                return new Label(userRoleService.getById(itemId.toString()).getName());
            }
        });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜