【问题标题】:Vaadin basic layout: fixed header and footer + scrollable contentVaadin 基本布局:固定页眉和页脚 + 可滚动内容
【发布时间】:2015-03-12 12:03:35
【问题描述】:

我是 Vaadin 的新手,我想知道它是否能满足我对 webapp 项目迁移的需求。 实际上,我已经在一个简单的目标上浪费了时间:拥有一个带有固定页眉和页脚的布局,以及中间的可滚动内容。 我对我想要的东西做了一个非常基本的小提琴: jsfiddle

这是我想出的主要 Vaadin 类:

public class MyVaadinUI extends UI {

// attributes

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "testvaadin.aep.com.AppWidgetSet")
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {

    buildMainLayout();
}

private void buildMainLayout() {

    final VerticalLayout mainLayout = new VerticalLayout();
    mainLayout.setSizeFull();

    //HEADER
    final VerticalLayout headerLayout = new VerticalLayout();
    final Resource res = new ThemeResource("img/logo.png");
    final Image image = new Image(null, res);
    headerLayout.addComponent(image);

    //CONTENT
    final VerticalLayout contentLayout = new VerticalLayout();
    for(int i=0; i<80; i++){
        contentLayout.addComponent(new Button("TEST " + i));
    }

    //FOOTER
    final VerticalLayout footerLayout = new VerticalLayout();
    footerLayout.addComponent(new Label("--------------------------- footer --------------------------"));

    mainLayout.addComponent(headerLayout);
    mainLayout.addComponent(contentLayout);
    mainLayout.addComponent(footerLayout);
    mainLayout.setExpandRatio(contentLayout, 1);
    setContent(mainLayout);
}

}

启动时显示的页面正常,但是当我向下滚动时,页脚也会滚动(它不是固定的)。

启动时:

滚动时:

我浏览了很多关于这个主题的页面,但我从来没有看到任何正确的答案。这在 Vaadin 中似乎相当复杂,尽管在 HTML 中非常简单; Vaadin 可能不适合我的需要。 无论如何,你知道我怎样才能实现这种行为吗? 谢谢!

【问题讨论】:

  • 如果您更喜欢摆弄 html,那么 CustomLayout 可能会有所帮助(请参阅 stackoverflow.com/questions/9563406/…)。
  • 您好,感谢您的回复,但实际上我不想为我的第一个要求定制。

标签: java layout header vaadin footer


【解决方案1】:

您可以使用Panel 创建可滚动的中心内容区域。请参阅下面的示例。

要使面板正常工作,组件层次结构中的所有内容都必须为 setSizeFull(或等效项),并且面板的内容不得(在示例中 mainLayoutcontentPanel 为 100%,但 contentLayout不是(隐式))

@Grapes([
        @Grab('org.vaadin.spring:spring-boot-vaadin:0.0.3'),
        @Grab('com.vaadin:vaadin-client-compiled:7.4.0.beta1'),
        @Grab('com.vaadin:vaadin-themes:7.4.0.beta1'),
       ])
import com.vaadin.ui.*

@org.vaadin.spring.VaadinUI
class MyUI extends UI {

    protected void init(com.vaadin.server.VaadinRequest request) {
        final headerLayout = new VerticalLayout(new Label('HEADER'))
        final footerLayout = new VerticalLayout(new Label('FOOTER'))

        final contentLayout = new VerticalLayout()
        80.times{ contentLayout.addComponent(new Button("TEST $it")) }
        // XXX: place the center layout into a panel, which allows scrollbars
        final contentPanel = new Panel(contentLayout)
        contentPanel.setSizeFull()

        // XXX: add the panel instead of the layout
        final mainLayout = new VerticalLayout(headerLayout, contentPanel, footerLayout)
        mainLayout.setSizeFull()
        mainLayout.setExpandRatio(contentPanel, 1)
        setContent(mainLayout)
    }

}

(使用 spring run vaadin.groovy 独立运行)

【讨论】:

  • 谢谢!这必须翻译成Java(不难......),但效果很好!我看到这里的要点是将 contentLayout 包装到一个面板中,因为我尝试了与您的相同的代码,但没有面板,它不起作用。太奇怪了,我在任何地方都找不到答案。
  • 完全正确。我在有趣的部分添加了一些 cmets。布局可能只会重新计算,如果 UI 也有变化(有时取决于主题),它们可能会有奇怪的溢出行为。另一种选择是使用CSSLayout 并处理CSS 中的设置。面板很棒,如果你不喜欢摆弄浏览器端的话。
猜你喜欢
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 2015-05-03
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多