Sangeeth S Kumar
Sangeeth S Kumar

Reputation: 142

Pass Custom Object between routes in Vaadin Flow

I am developing an Application in Vaadin Flow 14 with Separate Views for a Page. One is Create & Another is View.

Create is usual form style stuff which is fine. View page is something I am using a Vaadin Grid to render the saved data as part of API Call. So I am just planning to use a single Page with route name as view for all the view page requests and to execute the api calls based on a specific page identifier. My doubt is that how could I pass a parameter or a pojo object to my view route when clicking the view icon from my home page. I suppose the Before Enter can help, but not sure on the coding or if its possible.

Thanks in Advance.

Upvotes: 1

Views: 693

Answers (1)

Tatu Lund
Tatu Lund

Reputation: 10643

My doubt is that how could I pass a parameter

The most straightforward way to pass parameter(s) is to use url-parameters. The following pseudocode roughly shows the pattern. You can naturally also cache the bean to session attribute or SessionScoped bean store in order to avoid repeated fetch from the data base if there is tendency for the users to refresh this view.

@Route(value = "myroute", layout = MainLayout.class)
public class MyRoute implements HasUrlParameter<Integer>, AfterNavigationObserver {

    private Integer key;

    public MyRoute() {
        ... setup things like the form and binder ...
    }

    @Override
    public void afterNavigation(AfterNavigationEvent event) {
       if (key != null) {
          Bean bean = service.fetchBean(key);
       }
       binder.readBean(bean); // populate form with bean
    }

    @Override
    public void setParameter(BeforeEvent event,
            @OptionalParameter Integer parameter) {
        key = parameter;
    }
}

More about router: https://vaadin.com/learn/training/v14-router Documentation: https://vaadin.com/docs/v14/flow/routing/tutorial-routing-url-generation#navigation-target-with-hasurlparameter

You can use parameter in navigation for example like this

getUI().ifPresent(ui -> ui.navigate(MyRoute.class, key));

Upvotes: 2

Related Questions