How to fix object set in grid?

▼魔方 西西 提交于 2019-12-12 18:20:44

问题


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

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

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


回答1:


I see three options.

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

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

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
}



回答2:


You can configure which columns are shown in the grid by using grid.removeAllColumns() and then adding all columns you want to have in the grid with grid.addColumn(). Within addColumn() you can create a renderer that defines how the fields (coachName and playerSet) are displayed in the grid.

Let's have a class Team like

public class Team {
    private String coachName;
    private Set<Player> playerSet;
    private Set<Object> objects;
    //getters and setters
}

and a class Player like

public class Player {
    private String firstName;
    private String lastName;
    // getters and setters
}

Now you want to only have coach and player names in the grid. So (in my example) for coachName we can just use the field's getter and we can create a comma separated String for the playerSet with java streams easily.

Configure the grid like:

   grid.setItems(team);
   grid.removeAllColumns();
   grid.addColumn(new TextRenderer<>((ItemLabelGenerator<Team>) Team::getCoachName))
           .setHeader("Coach");
   grid.addColumn(new TextRenderer<>((ItemLabelGenerator<Team>) team1 -> team1.getPlayerSet().stream()
           .map(player1 -> player1.getFirstName() + " " + player1.getLastName())
           .collect(Collectors.joining(", "))))
           .setHeader("Players")
           .setFlexGrow(1);

Then the result looks like:



来源:https://stackoverflow.com/questions/55391791/how-to-fix-object-set-in-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!