简体   繁体   English

Vaadin:使用 Tab 组件在视图之间正确导航

[英]Vaadin: Propper navigation between views with Tab component

i have created a navigation menu bar with Vaadin and i was wondering how i can attach a view or a link to each tab so on click it can redirect me to the corresponding view.我用 Vaadin 创建了一个导航菜单栏,我想知道如何将视图或链接附加到每个选项卡,以便单击它可以将我重定向到相应的视图。 I have managed to get a workaround but i think this approach is flawed:我设法找到了解决方法,但我认为这种方法有缺陷:

Tabs tabs = new Tabs(new Tab("Login"), new Tab("Register"));
        tabs.setOrientation(Tabs.Orientation.VERTICAL);
        tabs.addSelectedChangeListener(event -> {
            if (event.getSelectedTab().getLabel().equalsIgnoreCase("Login")) {
                UI.getCurrent().navigate(LoginView.class);
            } else if (event.getSelectedTab().getLabel().equalsIgnoreCase("Register")) {
                UI.getCurrent().navigate(RegisterView.class);
            }
        });

I did not find an individual clickListener event on the Tab component which is weird to me.我没有在 Tab 组件上找到单独的 clickListener 事件,这对我来说很奇怪。 Also noticed that i can get the UI attached to the tab with tabName.getUI() method, however i cannot find a way to attach it.还注意到我可以使用 tabName.getUI() 方法将 UI 附加到选项卡上,但是我找不到附加它的方法。

Help me find the propper way to navigate with tabs!帮助我找到使用标签导航的正确方式! Thanks in advance!提前致谢!

In Vaadin 14 you can add components to Tabs.在 Vaadin 14 中,您可以向选项卡添加组件。 So instead of doing just new Tab("Login") you can do something like the follows:因此,您可以执行以下操作,而不是仅执行new Tab("Login")操作:

private Tab createMenuItem(String title, VaadinIcon icon, Class<? extends Component> target) {
    RouterLink link = new RouterLink(null,target);
    if (icon != null) link.add(icon.create());
    link.add(title);
    Tab tab = new Tab();
    tab.add(link);
    return tab;
}

Naturally RouterLink is not the only option, if you like you could use also Button and use click listener of the button to call navigation, etc. There is quite a lot options here.当然 RouterLink 不是唯一的选择,如果你喜欢你也可以使用 Button 并使用按钮的单击侦听器来调用导航等。这里有很多选项。

If you only want to show/hide different tab-pages without actual navigation, you can see how it's done in the tabs with pages example .如果您只想在没有实际导航的情况下显示/隐藏不同的标签页,您可以在带有页面示例标签中查看它是如何完成的。

If you want to navigate (which seems to be the case with your setup) then the addSelectedChangeListener is the correct approach - if you don't want to use RouterLink or Button as Tatus answer suggests.如果您想导航(您的设置似乎就是这种情况),那么addSelectedChangeListener是正确的方法 - 如果您不RouterLink Tatus 的回答所建议的那样使用RouterLinkButton Personally, I think the RouterLinks and Buttons do not really look nice inside the Tab component, and therefore prefer it this way instead.就个人而言,我认为 RouterLinks 和 Buttons 在 Tab 组件内部看起来并不好看,因此更喜欢这种方式。

To avoid having to compare hardcoded strings in that listener, you could use a Map<Tab, Class> similar to how it's done in the above link, and navigate to the class defined for that Tab.为避免在该侦听器中比较硬编码字符串,您可以使用类似于上述链接中的操作方式的Map<Tab, Class> ,并导航到为该 Tab 定义的类。

Tab loginTab = new Tab("Login");
Tab registerTab = new Tab("Register");

Map<Tab, Class> tabsMap = new HashMap<>();
tabsMap.put(loginTab, LoginView.class);
tabsMap.put(registerTab, RegisterView.class);

Tabs tabs = new Tabs(loginTab, registerTab);
tabs.setOrientation(Orientation.VERTICAL);
tabs.addSelectedChangeListener(event -> {
    UI.getCurrent().navigate(tabsMap.get(event.getSelectedTab()));
});

Edit: After testing it right now, I must say that using RouterLink inside Tab component does not look bad at all (I was thinking of Buttons, which DO look bad).编辑:现在测试之后,我必须说在 Tab 组件中使用RouterLink看起来一点也不糟糕(我在想按钮,它看起来很糟糕)。 So if you use navigation then use Tatu's answer, if you want to do showing/hiding of components without navigation, make a Map<Tab, Component> and show the component mapped to the selected tab (while hiding all other components that are mapped to other tabs)因此,如果您使用导航然后使用 Tatu 的答案,如果您想在没有导航的情况下显示/隐藏组件,请制作Map<Tab, Component>并显示映射到所选选项卡的组件(同时隐藏映射到的所有其他组件)其他标签)

paged-tabs add-on分页标签插件

Looks like Alejandro Duarte has made a Vaadin 14 add-on, named paged-tabs , for us to plug into our Vaadin projects.看起来Alejandro Duarte制作了一个名为paged-tabs的 Vaadin 14 插件,供我们插入到我们的 Vaadin 项目中。 I imagine this is a wrapper around the kind of code shown in the correct Answer by Kaspar Scherrer .我想这是对Kaspar Scherrer正确答案中显示的那种代码的包装。

This add-on simplifies the handling of tabs.此附加组件简化了选项卡的处理。 You just pass the components (widgets, layouts) along with a title.您只需将组件(小部件、布局)与标题一起传递即可。

PagedTabs tabs = new PagedTabs() ;
tabs.add( component , "Tab caption 1" ) ;
tabs.add( component2 , "Tab caption 2" ) ;

Alternatively, you can pass a Tab object.或者,您可以传递Tab对象。

Tab tab = new Tab();
tab.add( new Span( "Tab caption" ) , new Button( "Click me" ) ) ;

PagedTabs tabs = new PagedTabs() ;
tabs.add( new Span( "Tab content" ) , tab ) ;

This library also provide a “closable” feature, displaying an X for the user to click to remove that tab.该库还提供“可关闭”功能,显示一个X供用户单击以删除该选项卡。

分页标签小部件显示的标签的屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM