如何在Vaadin中创建垂直菜单?

huangapple go评论98阅读模式
英文:

How to create Vertical menu in Vaadin?

问题

我想创建一个Vaadin垂直菜单。目前我的菜单是水平排列的,像这样:

如何在Vaadin中创建垂直菜单?

对于这种视图,我在MainLayout.java类中有以下代码:

@Theme(value = Lumo.class)
@Route("")
@PWA(name = "Project Base for Vaadin", shortName = "Project Base", enableInstallPrompt = false)
@CssImport("./styles/shared-styles.css")
@CssImport(value = "./styles/menu-buttons.css", themeFor = "vaadin-button")
public class MainLayout extends AppLayout implements RouterLayout {

    public MainLayout() {
        final DrawerToggle drawerToggle = new DrawerToggle();
        drawerToggle.addClassName("menu-toggle");
        addToNavbar(drawerToggle);

        final VerticalLayout top = new VerticalLayout();
        //top.setDefaultVerticalComponentAlignment(Alignment.CENTER);

//        top.setClassName("menu-header");

        final Label title = new Label("Converted Application");
        top.add(title);
        addToNavbar(top);

        top.add(title);
        addToNavbar(top);

        // Navigation items
        addToDrawer(createMenuLink(HomeView.class, HomeView.VIEW_NAME,
                VaadinIcon.EDIT.create()));

        addToDrawer(createMenuLink(MainView.class, MainView.VIEW_NAME,
                VaadinIcon.INFO_CIRCLE.create()));

        addToDrawer(createMenuLink(ConvertedLoginPage.class, ConvertedLoginPage.VIEW_NAME,
                VaadinIcon.LOCK.create()));

        addToDrawer(createMenuLink(Table.class, Table.VIEW_NAME,
                VaadinIcon.LOCK.create()));
    }

    private RouterLink createMenuLink(Class<? extends Component> viewClass, String caption, Icon icon) {
        final RouterLink routerLink = new RouterLink(null, viewClass);
        routerLink.add(icon);
        routerLink.add(new Span(caption));
        icon.setSize("24px");
        return routerLink;
    }
}

请帮助我,如何将菜单项的水平视图转换为垂直视图?

英文:

I want to create Vaadin vertical menu. Currently my menu is in horizontal view like this:

如何在Vaadin中创建垂直菜单?

For that view I have code in MainLayout.java class:

@Theme(value = Lumo.class)
@Route(&quot;&quot;)
@PWA(name = &quot;Project Base for Vaadin&quot;, shortName = &quot;Project Base&quot;, enableInstallPrompt = false)
@CssImport(&quot;./styles/shared-styles.css&quot;)
@CssImport(value = &quot;./styles/menu-buttons.css&quot;, themeFor = &quot;vaadin-button&quot;)
public class MainLayout extends AppLayout implements RouterLayout {
public MainLayout() {
final DrawerToggle drawerToggle = new DrawerToggle();
drawerToggle.addClassName(&quot;menu-toggle&quot;);
addToNavbar(drawerToggle);
final VerticalLayout top = new VerticalLayout();
//top.setDefaultVerticalComponentAlignment(Alignment.CENTER);
//        top.setClassName(&quot;menu-header&quot;);
final Label title = new Label(&quot;Converted Application&quot;);
top.add(title);
addToNavbar(top);
top.add(title);
addToNavbar(top);
// Navigation items
addToDrawer(createMenuLink(HomeView.class, HomeView.VIEW_NAME,
VaadinIcon.EDIT.create()));
addToDrawer(createMenuLink(MainView.class, MainView.VIEW_NAME,
VaadinIcon.INFO_CIRCLE.create()));
addToDrawer(createMenuLink(ConvertedLoginPage.class, ConvertedLoginPage.VIEW_NAME,
VaadinIcon.LOCK.create()));
addToDrawer(createMenuLink(Table.class, Table.VIEW_NAME,
VaadinIcon.LOCK.create()));
}
private RouterLink createMenuLink(Class&lt;? extends Component&gt; viewClass, String caption, Icon icon) {
final RouterLink routerLink = new RouterLink(null, viewClass);
routerLink.add(icon);
routerLink.add(new Span(caption));
icon.setSize(&quot;24px&quot;);
return routerLink;
}
}

Please help me. how can I convert horizontal view of menu items into vertical view?

答案1

得分: 5

你可以创建一个垂直标签栏:

Tabs tabs = new Tabs();
tabs.setOrientation(Tabs.Orientation.VERTICAL);

请在此处找到完整的示例:

https://github.com/72services/jtaf4/blob/develop/src/main/java/ch/jtaf/ui/layout/MainLayout.java

英文:

You could create a vertical tab bar:

Tabs tabs = new Tabs();
tabs.setOrientation(Tabs.Orientation.VERTICAL);

Please find my full example here:

https://github.com/72services/jtaf4/blob/develop/src/main/java/ch/jtaf/ui/layout/MainLayout.java

答案2

得分: 3

你可以将你的RouterLinks添加到一个VerticalLayout中,然后将它添加到抽屉中。

// 导航项目
VerticalLayout routerLinks = new VerticalLayout();

routerLinks.add(createMenuLink(HomeView.class, HomeView.VIEW_NAME, VaadinIcon.EDIT.create()));
routerLinks.add(createMenuLink(MainView.class, MainView.VIEW_NAME, VaadinIcon.INFO_CIRCLE.create()));
routerLinks.add(createMenuLink(ConvertedLoginPage.class, ConvertedLoginPage.VIEW_NAME, VaadinIcon.LOCK.create()));
routerLinks.add(createMenuLink(Table.class, Table.VIEW_NAME, VaadinIcon.LOCK.create()));

addToDrawer(routerLinks);
英文:

You could add your RouterLinks into a VerticalLayout and add that into the drawer.

 // Navigation items
VerticalLayout routerLinks = new VerticalLayout();
routerLinks.add(createMenuLink(HomeView.class, HomeView.VIEW_NAME, VaadinIcon.EDIT.create()));
routerLinks.add(createMenuLink(MainView.class, MainView.VIEW_NAME, VaadinIcon.INFO_CIRCLE.create()));
routerLinks.add(createMenuLink(ConvertedLoginPage.class, ConvertedLoginPage.VIEW_NAME, VaadinIcon.LOCK.create()));
routerLinks.add(createMenuLink(Table.class, Table.VIEW_NAME, VaadinIcon.LOCK.create()));
addToDrawer(routerLinks);

huangapple
  • 本文由 发表于 2020年8月4日 14:30:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63241326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定