Vaadin(流):使用共享对象导航到目的地
我目前有一个显示 SomeModel
类型内容的网格.当我单击该 Grid
的条目时,我想导航到一个视图,该视图将对象作为其输入以显示条目内容.
I currently have a grid that displays content of type SomeModel
.
When I click an entry of that Grid
I would like to navigate to a view that takes an object as its input to display the entries content.
为了实现这种行为,我创建了一个 DetailLayout
,如下所示:
To achive this behaviour I created a DetailLayout
like this:
public DetailLayout extends FlexLayout implements HasUrlParameter<SomeModel>{
/* skipped some details */
@Override
public void setParameter(BeforeEvent event, Host parameter) {
/* This is where I expected to be able to handle the object */
}
}
从 Grid
我尝试像这样导航:
From within the Grid
I tried to navigate like this:
addSelectionListener((event) -> {
event.getFirstSelectedItem().ifPresent(somemodel -> {
getUI().ifPresent(ui -> {
ui.navigate(DetailLayout.class, somemodel);
});
});
});
但不幸的是,即使它的语法非常好,Vaadin 也不支持这种行为.
But unfortunately this behaviour is not supported by Vaadin even tho its syntax is perfectly fine.
您是否知道在导航时传递对象的另一种方法,或者我是否错过了官方文档的某个部分 文档 ?
Do you know of another way to pass an object while navigation or did I miss a certain part of the official documentation documentation ?
提前谢谢你
推荐答案
Key-Value集合
如其他答案的评论中所述,如果您不希望将 ID 值作为URL,然后使用 Vaadin 提供的键值集合在幕后工作.
Key-Value collection
As discussed in the comments on the other Answer, if you do not wish to expose the ID value as part of the URL, then work behind the scenes by using the key-value collection provided by Vaadin.
Vaadin 实际上提供了三个范围级别的键值集合:
Vaadin actually provides key-value collections at three levels of scope:
- 上下文
您的整个网络应用在运行时 - 会话
每个用户 - UI
每个网络浏览器窗口/标签,因为 Vaadin 支持多窗口网络应用程序
通过 getAttribute
& 在 VaadinContext
上可以使用应用范围的键值集合setAttribute
方法.
The app-wide key-value collection is available on the VaadinContext
, via getAttribute
& setAttribute
methods.
VaadinService.getCurrent().getContext().setAttribute( key , value ) ;
每个用户的键值集合在 VaadinSession
上可用,通过 getAttribute
&setAttribute
方法.
The per-user key-value collection is available on the VaadinSession
, via getAttribute
& setAttribute
methods.
VaadinSession.getCurrent().setAttribute( key , value ) ;
➥ 每个浏览器窗口/选项卡的集合(您在本问题中想要满足您的需求)并不那么容易获得.你必须经过一个间接的步骤.在 ComponentUtil
类,调用 setData
&getData
方法.除了 传递你的key和你的值,传递当前的UI
对象.
➥ The per-browser-window/tab collection (what you want for your needs in this Question) is not quite so readily available. You have to go through an indirect step. On the ComponentUtil
class, call setData
& getData
methods. In addition to passing your key and your value, pass the current UI
object.
Component c = UI.getCurrent() ;
String key = "com.example.acmeapp.selectedProductId" ;
Object value = productId ;
ComponentUtil.setData( c , key , value ) ;
<小时>
请投票给我的 ticket #6287,一个功能请求添加 <UI
类的 code>setAttribute/getAttribute
方法,以匹配 VaadinSession
和 VaadinContext
的方法.
Please vote for my ticket # 6287, a feature-request to add setAttribute
/getAttribute
methods on UI
class, to match those of VaadinSession
and VaadinContext
.
相关文章