Vaadin8 网格嵌套 属性 错误
Vaadin8 grid nested property error
我正在尝试创建一个带有嵌套 属性 的网格。但是我找不到在网格列中显示嵌套 属性 字段的方法。
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;
}
我只想在网格中显示 user.username。但是在这段代码中,显示所有用户对象堆栈引用否则。
像这样: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;
}
我正在尝试创建一个带有嵌套 属性 的网格。但是我找不到在网格列中显示嵌套 属性 字段的方法。
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;
}
我只想在网格中显示 user.username。但是在这段代码中,显示所有用户对象堆栈引用否则。
像这样: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;
}