Vaadin 从 RouterLayout 引用 mainLayout
Vaadin Refer to mainLayout from RouterLayout
我需要在应用程序屏幕之间创建一个缓冲区。
我想在 mainLayout 中创建一个缓冲区,但我无法从子层访问它
我试过'static',然后缓冲区对所有用户都是通用的,这是不正确的
Cookies也不顺table因为数据结构复杂
带有静态缓冲区的代码:
例子中,创建了一个table,当你table中的select个元素时,它们被写入缓冲区,如果缓冲区中有元素,它们标记在 table
MainLayout.java
public class MainLayout extends AppLayout implements RouterLayout {
public static Set<Test> buffer;
public MainLayout() {
buffer = new HashSet<Test>();
/*...*/
}
}
BasicView.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);
}
/*...*/
}
}
您实际上可以遍历布局的父布局直到主布局。例如。你可以做下面的事情
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 引导,将此缓冲区作为 VaadinSessionScoped bean 并在需要时自动装配它会更自然。下一版本的 Vaadin 还将添加特定的 RouteScope,如果需要,它允许更精确的范围界定。
另请参阅旧 Vaadin 论坛对此的讨论:https://vaadin.com/forum/thread/17917385/vaadin-14-accessing-components-of-mainview
我需要在应用程序屏幕之间创建一个缓冲区。 我想在 mainLayout 中创建一个缓冲区,但我无法从子层访问它
我试过'static',然后缓冲区对所有用户都是通用的,这是不正确的
Cookies也不顺table因为数据结构复杂
带有静态缓冲区的代码:
例子中,创建了一个table,当你table中的select个元素时,它们被写入缓冲区,如果缓冲区中有元素,它们标记在 table
MainLayout.java
public class MainLayout extends AppLayout implements RouterLayout {
public static Set<Test> buffer;
public MainLayout() {
buffer = new HashSet<Test>();
/*...*/
}
}
BasicView.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);
}
/*...*/
}
}
您实际上可以遍历布局的父布局直到主布局。例如。你可以做下面的事情
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 引导,将此缓冲区作为 VaadinSessionScoped bean 并在需要时自动装配它会更自然。下一版本的 Vaadin 还将添加特定的 RouteScope,如果需要,它允许更精确的范围界定。
另请参阅旧 Vaadin 论坛对此的讨论:https://vaadin.com/forum/thread/17917385/vaadin-14-accessing-components-of-mainview