Vaadin 表隐藏列和容器定制

发布于 2024-08-25 15:31:01 字数 1319 浏览 2 评论 0原文

我正在使用 Vaadin 和 Hibernate 测试一个项目。我正在尝试使用 HbnContainer 类将数据显示到表中。问题是我不想在表中显示两个类的所有属性。

例如:

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

第二个类:

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

//getters and setters
}

接下来,我使用 HbnContainer 检索数据,并将其连接到表:

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

表将仅显示来自 User 的列,对于“角色”,它将显示角色 id。如何隐藏该列并将其替换为 UserRole.name ?

我设法使用 ColumnGenerator() 获取表中 UserRole 的字符串值 - 但我无法删除带有数值的前一列。

我缺少什么?或者,在显示表格之前“自定义”数据的最佳方法是什么(如果我想在表格中显示来自多个对象类型的数据......我该怎么办?)

如果我找不到简单的解决方案很快,我想我只会“手工”构建表格。

那么,关于这个问题有什么建议吗?

编辑:
我之前没有很好地表达自己。我需要知道的是如何将嵌套的 pojo 与 HbnContainer 一起使用,并控制表中出现的属性(和“子属性”)。我尝试扩展和重新实现 HbnContainer 的某些部分,但是..无法正确执行。

对于前面的示例,从 Users 表生成的表如下所示:

Name  |Role
George| 1
Alex  | 2 

我想要类似的内容:

Name  | Role
George| admin
Alex  | user

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
George| admin
Alex  | user

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

っ左 2024-09-01 15:31:01

您想要做的是定义哪些属性应该在表中可见(从而排除角色 ID)。这可以通过 来实现setVisibleColumns() 方法。

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.

倚栏听风 2024-09-01 15:31:01

您可以重写 Table 类中的 formatPropertyValue 方法,而不是隐藏列/添加生成的列。

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

要获取 POJO(在您的情况下为 User 对象),您可以使用以下构造:

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

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();
居里长安 2024-09-01 15:31:01

我遇到了和你一样的问题,这对你有帮助(希望它也能帮助你)

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

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());
            }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文