Vaadin8网格嵌套属性错误

时间:2017-11-05 10:36:41

标签: java spring-boot vaadin8 nested-properties

我正在尝试使用嵌套属性创建网格。但我找不到在网格列中显示嵌套属性字段的方法。

    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

1 个答案:

答案 0 :(得分:0)

此解决方案有一种简单的方法。如果我们定义 Grid&lt; \ Branch /&gt; 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;
    }