无法在 Vaadin 8 中增加网格的宽度

问题描述

public MyTabbedForm() {
        this.refresh = new Button("");
        this.refresh.addClickListener(buttonClickEvent -> {
            this.grid.setItems(getCompany());
   
        });
        this.grid = new Grid<>();
        this.grid.setWidthFull();
        this.grid.addColumn(Company::getName).setHeader("Name");
        this.grid.addColumn(company -> (company. getCompanyCode()))
                .setHeader("Company Code");
        this.grid.addColumn(company -> (company. getCompanyId()))
        .setHeader("Company Id");
        this.grid.addColumn(company -> (company. getCompanyStatus()))
        .setHeader("Company Status");
        this.grid.getColumns().forEach(col -> col.setAutoWidth(true));
        add(this.refresh,this.grid);
    }

但我可以根据内容增加列的宽度

解决方法

您将 Grid 设置为全宽。在 Vaadin 8 中,这意味着 Grid 应该占据布局中插槽的完整大小。假设您在 VerticalLayout 中有一个宽度为“100px”的网格,并且您将 Grid 设置为全宽,它的宽度将为“100px”。我看到您的 Grid 在某些父布局中,这可能在某些大父布局中,等等。您需要遵循该链,那里也设置了正确的宽度。

另请查看 Layouting basics 培训视频。

,

在 MyTabbedForm 构造函数的末尾尝试添加以下行

    Component parent = this.grid.getParent();
    while (null != parent) {
        if (parent instanceof AbstractComponent) {
            parent.setWidthFull();
        }
        parent = getParent();
    }

这会将宽度设置为网格的所有祖先的 100%。