【问题标题】:Vaadin 8: can you right align a Button in a HorizontalLayout component cell of a TreeGrid component column?Vaadin 8:您可以在 TreeGrid 组件列的 Horizo​​ntalLayout 组件单元格中右对齐按钮吗?
【发布时间】:2021-06-01 20:24:39
【问题描述】:

我有一个包含 2 个组件列的 TreeGrid。第一个组件列包含一个带有标签和按钮的 Horizo​​ntalLayout。第二个组件列包含一个带按钮的 Horizo​​ntalLayout。我无法右对齐第一个组件列中的 Button。可行吗?如果它不可行,您有任何解决方法建议吗?我的约束是 TreeGrid,两列,第一列应该包含一个标签和一个右对齐的按钮。 到目前为止我尝试了什么

TreeGrid<Object> treeGrid = new TreeGrid<>();
treeGrid.setSizeFull();
treeGrid.addComponentColumn(vp -> {
    Button button = new Button("button");
    HorizontalLayout cell = new HorizontalLayout(new Label("label"), button);
    cell.setSizeFull();
    cell.setExpandRatio(button, 1.0f);
    cell.setComponentAlignment(button, Alignment.MIDDLE_RIGHT);
    return cell;
}).setCaption("st column").setExpandRatio(1).setId("st column");
treeGrid.addComponentColumn(vp -> new HorizontalLayout(new Button("some other button")))
    .setCaption("nd column").setId("nd column").setWidth(200.0d);
TreeData<Object> objectTreeData = new TreeData<>();
objectTreeData.addRootItems(new Object());
treeGrid.setDataProvider(new TreeDataProvider<>(objectTreeData));
window.setContent(treeGrid);
UI.getCurrent().addWindow(window);
window.center();
window.setWidth("40%");
window.setHeight("40%");

输出在附件中。

【问题讨论】:

    标签: vaadin vaadin8


    【解决方案1】:

    看起来节点没有设置宽度,Horizo​​ntalLayout 的整个宽度取自节点而不是整个单元格的宽度。

    作为一个有点老套的解决方法,您可以使用 StyleGenerator 为列指定样式名称 (column.setStyleGenerator(item -&gt; "myColumn");),然后将类似 ".myColumn .v-treegrid-node {width:100%;} 的内容添加到您的主题中。如果您的主题现在将内容稍微推得太远,您也可以添加一些填充来抵消这种情况:.myColumn .v-horizontallayout {padding-right: 10px;}"

    如果您添加一些层次结构,您可能需要为每个级别添加更多填充:.myColumn .depth-1 .v-horizontallayout {padding-right: 26px;}.myColumn .depth-2 .v-horizontallayout {padding-right: 42px;} 等等(实际值取决于您的主题)。使用普通 Valo 会在每个深度级别添加 1em (16px) 的缩进,这是您需要解决的问题。

    对于更复杂的 SASS 解决方案,请参阅Valo does the indenting。请记住将深度 0 级别的基本填充添加到计算中。

    【讨论】:

    • 你好!谢谢你的建议。它仅适用于我树中的根节点。我相信子节点有自己的填充。有没有办法记住子节点的填充以及一般后代节点的填充?
    • 编辑了我的答案以添加关卡样式。
    • GitHub 上也有关于这个问题的进一步讨论,所以为了清楚起见交叉链接:github.com/vaadin/framework/issues/12319
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 2018-04-09
    • 2011-05-03
    相关资源
    最近更新 更多