如何修复网格中的对象集? [英] How to fix object set in grid?

查看:84
本文介绍了如何修复网格中的对象集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个类似的类:

In my application i have a class like:

public class Team {
    private Country teamId;
    private Set<Player> playerSet;
    private Set<Player> substitutes;
    private Set<Coach> coachSet; 
}

当我实例化一个网格时:

When i instantiate a grid like:

Grid<Team> grid = new Grid<>(Team.class);

并从数据库中设置allTeam(),它显示playerSet和coachSet的对象.

and set allTeam() from database it shows object for playerSet and coachSet.

我的问题是我只想显示由或\ n串联的球员姓名和教练姓名.

My question is i just want to show players name and coach name concate by ,or \n.

任何想法我该怎么做?作为一个初学者,这对我来说很复杂

Any idea how can i do that?As a beginner it is complicated for me

推荐答案

我看到三个选项.

第一个选项是您已经发现的一个选项:将它们的名称连接在单个String中.可以这样完成:

The first option is the one you already found yourself: concatenate their names in a single String. This can be done like this:

grid.addColumn(team -> {
    Set<String> coachNames = new HashSet<>();
    for (Coach coach : team.getCoaches()){
        coachNames.add(coach.getName());
    }
    return String.join(", ", coachNames);
});


第二个方法是利用Grid项目的详细信息-您可以在项目详细信息中显示coachs网格.由于您想同时显示教练和球员,因此此选项可能不是最好的选择,但我想提一提这种可能性. (可以在项目详细信息内放置两个网格,但这很奇怪.不是最佳的用户体验.)


The second one would be to make use of the Grid item Detail - you could show a coaches grid in the item details. Since you want to display both coaches and players, this option is probably not the best but I wanted to mention the possibility. (Placing two grids inside the item details is possible, but quite strange. Not optimal user experience.)

grid.setItemDetailsRenderer(new ComponentRenderer<>(team -> {
    Grid<Coach> coachGrid = new Grid<>(Coach.class);
    coachGrid.setItems(team.getCoaches());
    return coachGrid;
}));


第三个选择是将团队网格放置在视图的一侧,在另一侧上显示团队网格中所选项目的一些相关内容.您可以为教练提供一个单独的网格,为球员提供一个网格,为替代者提供一个网格.如果愿意,您也可以将此团队详细信息布局实现为单独的视图.如果您的Team对象随着更多的集合,集合和其他相关属性而变得更加复杂,则此选项将变得更具吸引力,因为它具有很好的可伸缩性.


A third option would be to have the team grid on one side of the view, and on the other you show some relevant stuff of the selected item of the team grid. You can have a separate Grid for the coaches, one for the players, one for the substitutes. You could implement this team detail layout also as a separate view if you wish. If your Team object will get more complicated with more sets, collections and other relative properties, the more will this option become appealing, as this is quite scalable/expandable.

grid.addSelectionListener(event -> {
    if(event.getFirstSelectedItem().isPresent()){
        buildTeamDetails(event.getFirstSelectedItem().get())
    }
})

private void buildTeamDetails(Team team){
    // build your team detail layouts here
}

这篇关于如何修复网格中的对象集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆