Vaadin 从 RouterLayout 中引用 mainLayout [英] Vaadin Refer to mainLayout from RouterLayout

查看:33
本文介绍了Vaadin 从 RouterLayout 中引用 mainLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在应用程序屏幕之间创建一个缓冲区.我想在 mainLayout 中创建一个缓冲区,但我无法从子层访问它

I need to create a buffer between the application screens. I think to make a buffer in the mainLayout but I can't access it from the child layers

我尝试通过'static'来做到这一点,然后缓冲区对所有用户都是通用的,这是不正确的

I tried to do it through 'static', then the buffer is common for all users, and this is incorrect

Cookies也不适合,因为数据结构复杂

Cookies are also unsuitable because the data structure is complex

带有静态缓冲区的代码:

Code with static buffer:

示例中创建了一个表,当你选择表中的元素时,将它们写入缓冲区,如果缓冲区中有元素,则在表中进行标记

in the example, a table is created, when you select elements in the table, they are written to the buffer, and if there are elements in the buffer, they are marked in the table

MainLayout.java

MainLayout.java

public class MainLayout extends AppLayout implements RouterLayout {


    public static Set<Test> buffer;

    public MainLayout() {
        buffer = new HashSet<Test>();
        /*...*/
    }
}

基本视图.java

@Route(value = "BasicView", layout = MainLayout.class)
@RouteAlias(value = "/BasicView", layout = MainLayout.class)
public class BasicView extends VerticalLayout {

    private final Grid<Test> tests;

    public BasicView(@Autowired TestService){
        /*...*/
        tests = new Grid<>(Test.class, false);
        tests.addSelectionListener(event -> {
        MainLayout.buffer = event.getAllSelectedItems();});
        /*...*/
        for(Test el : MainLayout.buffer)
        {
           tests.select(el);
        }
        /*...*/
    }
}

推荐答案

您实际上可以遍历布局的父级直至主布局.例如.你可以在下面做一些事情

You can actually traverse parents of the layout upto main layout. E.g. you can do something lie below

    Optional<Component> parent = this.getParent();
    Buffer buffer = null;
    while (parent.isPresent()) {
        Component p = parent.get();
        if (p instanceof MainLayout) {
            MainLayout main = (MainLayout) p;
            buffer = main.getBuffer();
        }
        parent = p.getParent();
    }

但是我不确定这是否是最好的方法.如果你碰巧使用例如Spring Boot,将此缓冲区作为 VaadinSessionScoped bean 并在需要时自动装配它会更自然.Vaadin 的下一个版本还将添加特定的 RouteScope,以便在需要时进行更多的精确范围界定.

However I am not sure if it is the best approach. If you happen to use e.g. Spring Boot, it would be more natural to have this buffer as VaadinSessionScoped bean, and Autowire it where needed. The next version of Vaadin will also add specific RouteScope, which allows more pin-point scoping if needed.

另见旧的 Vaadin 论坛讨论:https://vaadin.com/forum/thread/17917385/vaadin-14-accessing-components-of-mainview

See also old Vaadin Forum discussion about this: https://vaadin.com/forum/thread/17917385/vaadin-14-accessing-components-of-mainview

这篇关于Vaadin 从 RouterLayout 中引用 mainLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆