Show TOC

Methods and Events for NavigationLocate this document in the navigation structure

SAPUI5 provides several methods and events for navigation.

The most general sequence of navigation is that the hash is changed (e. g. because the user clicks on something which leads to a navigation), this leads to pattern matching which will then notify the attached handlers which have to react accordingly.

navTo

Use this method to navigate to the given route and fill the hash with the corresponding data. The listener callbacks of controllers listening to this route are provided with data. The method uses takes the following parameters:

  • name

    Name of the route parameter

  • parameters

    Route parameters

  • replace

    Defines if the hash should be replaced (no browser history entry) or set (browser history entry)

sap.ui.controller("MyApp.View2", {
  anyEvent: function() {
        sap.ui.core.UIComponent.getRouterFor(this).navTo("product",{ id: "5", productId: "3"});
    }
});

When changing the hash, all listeners to this hash are informed.

attachRouteMatched

The route matched event is fired every time a changed hash matches a route or a pattern. If you want to only react to specific routes, check if the name parameter matches the route you want to listen to. The event has the following parameters:

  • name

    Name of the route that has matched

  • arguments

    Object of arguments that are part of the route, mainly the parameters of the hash

  • targetControl

    Control instance of the targetControl if given

  • view

    Instance of the view, if a view was inserted

sap.ui.controller("MyApp.View1", {
    onInit: function() {
        var oRouter = sap.ui.core.routing.Router.getRouter("appRouter");
        oRouter.attachRouteMatched(function(oEvent) {
            if (oEvent.getParameter("name") !== "view1") {
                            return:
                        }
            this._selectItemWithId(oEvent.getParameter("arguments").id);
        }, this);
       },
    _selectItemWithId : function(id) {
        //implementation
       }
attachRoutePatternMatched

This event is similar to route matched, but it only fires for the route that has a matching pattern, and not for its parent route. This means, if a subroute matches, the attachRoutePatternMatched event is only fired for the subroute whereas the attachRouteMatched event is fired for the subroute as well as for the parent. You still have to check for the name of the route inside your listener function.

viewCreated

A viewCreated event is fired every time a new view has been created by the navigation system. The event has the following parameters:

  • view

    View instance

  • viewName

    Name of the view

  • type

    View type