Reputation: 576
In my Vaadin application I have two layouts in the main view. Each layout defined in a separate class. The top one contains some instructions and a bunch of buttons. The bottom is a complex list with couple buttons in each element as well. Each button in their opens dialogs, which are also defined in the respective classes. Results of these dialogs need to be passed to both layouts and acted upon, i.e. enabling/disabling some buttons, adding/deleting/updating data in the list, filtering, etc.
I already wrote all layout and dialog classes (over 2000 lines of Java code) but now having problems making them all work together.
I was thinking about using DataChangeEvent but after reviewing documentation I am not so sure about it. As I understood, DataChangeEvent is used to notify UI about changes data on the server. My case is different. For example, click on filter button will spawn a dialog that produces an instance of Filter class. That object need to be passed to the bottom layout and processed there - filter content of the list according to the object properties.
I did search through documentation and forum posts but couldn't find anything I could use. If anyone has done something similar, I would appreciate some guidance.
I am using OpenJDK 14 and Vaadin 23.1
Upvotes: 0
Views: 181
Reputation: 10643
There are three intended mechanisms in Vaadin to do this.
Route parameters, which can be defined either as url template or implementing HasUrlParameter
interface. This will allow e.g. you to navigate to the view using id of the object as a parameter. Then the target view can fetch the object e.g. in afterNavigation
callback. Here is some rough idea:
@Route(value = "greet")
public class UserEditor extends Div
implements HasUrlParameter<Integer>, AfterNavigationObserver {
...
@Override
public void setParameter(BeforeEvent event, Integer id) {
this.id = id;
}
@Override
public void afterNavigation(AfterNavigationEvent event) {
this.user = id != null ? myService.fetchItem(id) : new User();
binder.readBean(this.user);
}
}
Query parematers can be used in similar fashion.
Use new Vaadin 23.2 feature which allows you to use modification callback in the navigate method. I.e. you can perform method call to target view after navigation.
new Button("Edit " + user.getName(), event -> {
ui.navigate(UserEditor.class)
.ifPresent(editor -> editor.editUser(user));
})
The method 1. is preferable if you want this to be bookmarkable. And method 3. is good for just exact opposite, i.e. when you do not want to expose id in url.
Upvotes: 1