我正在尝试创建一个带有嵌套属性的网格。但是我找不到在网格列中显示嵌套属性字段的方法。
private Component buildGrid() {
branchGrid = new Grid<>(Branch.class);
branchGrid.setItems(DummyData.allBranches);
branchGrid.setSelectionMode(Grid.SelectionMode.SINGLE);
branchGrid.setSizeFull();
branchGrid.getEditor().setEnabled(false);
branchGrid.addComponentColumn((ValueProvider<Branch, Component>) branch -> {
Button btnEdit = new Button(VaadinIcons.EDIT);
btnEdit.addStyleName(MaterialTheme.BUTTON_CUSTOM + " " + MaterialTheme.BUTTON_BORDER);
btnEdit.setDescription("Click for edit!");
return btnEdit;
}).setId("edit");
branchGrid.setColumnOrder("edit", "branchId", "branchName", "email", "phone", "location", "owner", "active");
return branchGrid;
}
public class Branch {
private String branchId; //yymmddHHmmSSS
private String branchName;
private String email;
private String phone;
private String location;
private User owner;
private boolean active;
}
public class User {
private long userId;
private String username;
private String password;
private String email;
}
我只想在网格中显示用户用户名。但在此代码中,显示所有用户对象堆栈引用,否则。
像这样:com.meyrasoft.checkupbox.model.User@12703bc
此解决方案有一个简单的方法。如果我们定义 Grid <\Branch> branchGrid,我们应该使用 addColumn() 定义,如下所示。
private Component buildGrid() {
Grid<Branch> branchGrid = new Grid<>();
branchGrid.setItems(DummyData.allBranches);
branchGrid.setSelectionMode(Grid.SelectionMode.SINGLE);
branchGrid.setSizeFull();
branchGrid.getEditor().setEnabled(false);
branchGrid.addComponentColumn((ValueProvider<Branch, Component>) branch -> {
Button btnEdit = new Button(VaadinIcons.EDIT);
btnEdit.addStyleName(MaterialTheme.BUTTON_CUSTOM + " " + MaterialTheme.BUTTON_BORDER);
btnEdit.setDescription("Click for edit!");
return btnEdit;
}).setId("edit");
branchGrid.addColumn(b -> b.getBranchId()).setCaption("Branch Id");
branchGrid.addColumn(b -> b.getBranchName()).setCaption("Branch Name");
branchGrid.addColumn(b -> b.getEmail()).setCaption("E-Mail");
branchGrid.addColumn(b -> b.getPhone()).setCaption("Phone");
branchGrid.addColumn(b -> b.getLocation()).setCaption("Location");
branchGrid.addColumn(b -> String.valueOf(b.getOwner().getUsername())).setCaption("Owner");
branchGrid.addColumn(b -> b.isActive()).setCaption("Active Status");
return branchGrid;
}