問題描述
我目前有一個顯示 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
.
這篇關于Vaadin(流):使用共享對象導航到目的地的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!