改变vaadin中的样式定义

时间:2016-10-10 15:44:40

标签: css vaadin7

我的代码会动态隐藏组件;它使用[component] .addStyleName(" name")向组件添加样式,并定义该样式以隐藏组件。

我有一个包含大量组件的页面;我可以将它们放在一个数组中并执行此操作,但我希望以不同的方式。我想将所有这些组件分配给他们自己的风格 - 例如" costPanel" - 然后使用服务器端的vaadin代码来改变样式的定义" costPanel"在运行时。

Vaadin中的Page.Styles类没有获取现有样式的方法,也没有改变那些样式的方法 - 唯一的方法是添加它们。

这是否可能在Vaadin,即使我必须在客户端做一些事情呢?

2 个答案:

答案 0 :(得分:2)

这可能最适合作为评论,但它并不适合那里。

不是试图光顾,但听起来你正在以非常复杂的方式尝试重新发明轮子component.setVisible(false)将完全满足您的需求,因为组件不会占用任何空间,因为它实际上并不存在于DOM本身。看一下下面的例子:

代码:

public class LayoutWithInvisibleComponents extends VerticalLayout {
    private int index = 0;

    public LayoutWithInvisibleComponents() {
        // add a visibility toggling button
        addComponent(new Button("Toggle next", event -> {
            Component component = getComponent(++index);
            if (component instanceof Button) {
                // just toggle the next one if it's a button
                component.setVisible(!component.isVisible());
            }
            if (index == getComponentCount() - 1) {
                // reset counter
                index = 0;
            }
        }));

        // add some invisible dummy buttons
        for (int i = 0; i < 5; i++) {
            Button button = new Button("Button " + i);
            button.setVisible(false);
            addComponent(button);
        }

        // and add a visual delimiter
        Panel rightPanel = new Panel(new Label("---------- some visual delimiter ----------"));
        rightPanel.setSizeFull();
        addComponent(rightPanel);
    }
}

结果:

DOM modifications

还有什么我想念的吗?

答案 1 :(得分:1)

这也可以做出更好的评论,但在那里并不合适。

以下内容来自Vaadin书:

Beware that invisible beings can leave footprints. The containing layout cell that holds the invisible
component will not go away, but will show in the layout as extra empty space. Also expand ratios
work just like if the component was visible - it is the layout cell that expands, not the component.

短语&#34;在布局中显示为额外的空白空间&#34;让我确信我的组件应该是空白的,开放的,背景色的空间。我不记得我是否尝试过,但我可能有一些其他错误导致我得出结论我的假设是正确的,并且该设置是为了使其无渲染,但空间仍然可见。 / p>

Vaadin拥有比大多数行业更好的文档,但在这种情况下我得到了意义。在接下来的段落中,他们甚至还有其他解释说明了我通过这个问题所学到的东西,但这里引用的部分似乎与之相矛盾。