Show TOC

Step 7: Navigate to Routes with Mandatory ParametersLocate this document in the navigation structure

In this step, we implement a feature that allows the user to click on an employee in the list to see additional details of the employee. A route pattern can have one or more mandatory parameters to identify objects in an app.

The detail page has to read the ID of the employee from the URL to fetch and display the employee data from the server. If the employee was not found, for example, because an invalid employee ID was passed on, we want to inform the user by displaying the notFound target. Of course, the back navigation has to work as well for this page.

Figure 1: Employee list with navigation option for items
Figure 2: Detail Page for a selected employee
Figure 3: Not Found page for an invalid EmployeeID

You can view and download all files in the Explored app in the Demo Kit under Routing and Navigation - Step 7.

Figure 4: Folder structure for this step
	"_version": "1.1.0",
	"": {
	"sap.ui": {
	"sap.ui5": {
		"routing": {
			"config": {
				"routerClass": "sap.m.routing.Router",
				"viewType": "XML",
				"viewPath": "sap.ui.demo.nav.view",
				"controlId": "app",
				"controlAggregation": "pages",
				"transition": "slide",
				"bypassed": {
					"target": "notFound"
			"routes": [{
				"pattern": "",
				"name": "appHome",
				"target": "home"
			}, {
				"pattern": "employees",
				"name": "employeeList",
				"target": "employees"
			}, {
				"pattern": "employees/{employeeId}",
				"name": "employee",
				"target": "employee"
			"targets": {
				"home": {
					"viewName": "Home",
					"viewLevel" : 1
				"notFound": {
					"viewName": "NotFound",
					"transition": "show"
				"employees": {
					"viewPath": "sap.ui.demo.nav.view.employee",
					"viewName": "EmployeeList",
					"viewLevel" : 2
				"employee": {
					"viewName": "employee.Employee",
					"viewLevel" : 3

From our data model (webapp/localService/metadata.xml or webapp/localService/mockdata/Employees.json), you can see that each employee entity is identified by an EmployeeID. We define a new route that expects a mandatory employeeId in its pattern to address an employee. Unlike the patterns we used before, this pattern has a dynamic part. We create a new route employee and use employees/{employeeId} as its pattern.

The {employeeId} part of the pattern is a mandatory parameter as indicated by the curly brackets. The hash that contains an actual employee ID is matched against that pattern at runtime.

The following hashes would match in our case: employees/2, employees/7, employees/anInvalidId, and so on. However, the hash employees/ will not match as it does not contain an ID at all. The target of our route is employee. We create the target employee with viewLevel 3. With that, we make sure that we have the correct slide animation direction.

Next, we have to create the view employees.Employee; for better illustration the viewPath is not specified this time.

webapp/view/employee/Employee.view.xml (New)
		title="{i18n>EmployeeDetailsOf} {FirstName} {LastName}"
				class="sapUiResponsiveMargin sapUiNoContentPadding">
						<Title text="{i18n>EmployeeIDColon} {EmployeeID}" level="H2"/>
						<ToolbarSpacer />
						labelSpanL="3" labelSpanM="3" emptySpanL="4" emptySpanM="4"
						columnsL="1" columnsM="1">
							<Label text="{i18n>FirstName}" />
							<Text text="{FirstName}" />
							<Label text="{i18n>LastName}" />
							<Text text="{LastName}" />
							<Label text="{i18n>Address}" />
							<Text text="{Address}" />
							<Label text="{i18n>City}" />
							<Text text="{City}, {Region}" />
							<Label text="{i18n>PostalCode}" />
							<Text text="{PostalCode}" />
							<Label text="{i18n>PhoneHome}" />
							<Text text="{HomePhone}" />
							<Label text="{i18n>Country}" />
							<Text text="{Country}" />

Create the file Employee.view.xml inside the webapp/view/employee folder. This employee view displays master data for an employee in a panel with a SimpleForm control: first name, last name and so on. The data comes from a relative data binding that is set on the view level as we can see in the controller later. As we are focusing on the navigation aspects in this tutorial, we won’t go into detail on the controls of the view. Just copy the code.

webapp/controller/employee/Employee.controller.js (New)
], function (BaseController) {
	"use strict";
	return BaseController.extend("sap.ui.demo.nav.controller.employee.Employee", {
		onInit: function () {
			var oRouter = this.getRouter();
			oRouter.getRoute("employee").attachMatched(this._onRouteMatched, this);
			// Hint: we don't want to do it this way
			oRouter.attachRouteMatched(function (oEvent){
				var sRouteName, oArgs, oView;
				sRouteName = oEvent.getParameter("name");
				if (sRouteName === "employee"){
			}, this);
		_onRouteMatched : function (oEvent) {
			var oArgs, oView;
			oArgs = oEvent.getParameter("arguments");
			oView = this.getView();

				path : "/Employees(" + oArgs.employeeId + ")",
				events : {
					change: this._onBindingChange.bind(this),
					dataRequested: function (oEvent) {
					dataReceived: function (oEvent) {
		_onBindingChange : function (oEvent) {
			// No data for the binding
			if (!this.getView().getBindingContext()) {

Now we create the file Employee.controller.js in the webapp/controller/employee folder. In this controller file, we want to detect which employee shall be displayed in order to show the employee’s data in the view. Therefore, we query the router for the route employee and attach a private event listener function _onRouteMatched to the matched event of this route.

In the event handler, we can access the arguments parameter from the oEvent parameter that contains all parameters of the pattern. Since this listener is only called when the route is matched, we can be sure that the mandatory parameter employeeId is always available as a key in arguments; otherwise the route would not have matched. The name of the mandatory parameter employeeId correlates to the {employeeId} from our pattern definition of the route employee and thus to the value in the URL.

In _onRouteMatched we call bindElement() on the view to make sure that the data of the specified employee is available in the view and it’s controls. The ODataModel will handle the necessary data requests to the back end in the background. While the data is loading, it would be nice to show a busy indicator by simply setting the view to busy. Therefore, we pass an events object to bindElement() to listen to the events dataRequested and dataReceived. The attached functions handle the busy state by calling oView.setBusy(true) and oView.setBusy(false) respectively.

We also add an event handler to the change event as a private function _onBindingChange. It checks if the data could be loaded by querying the binding context of the view. As seen in the previous steps, we will display the notFound target if the data could not be loaded.

Instead of calling attachMatched(…) on a route we could also call attachRouteMatched(…) directly on the router. However, the event for the latter is fired for every matched event of any route in the whole app. We don’t use the latter because we would have to implement an additional check for making sure that current route is the route that has been matched. We want to avoid this extra overhead and register on the route instead.

	<Page id="employeeListPage" title="{i18n>EmployeeList}"
			<List id="employeeList" headerText="{i18n>ListOfAllEmployees}" items="{/Employees}">
						title="{FirstName} {LastName}"

It’s time to change the EmployeeList view so that we can navigate to the new view. We set the attribute type of the StandardListItem template to Navigation to make the item clickable and indicate a navigation feature to the user. Additionally, we add an event handler for the press event that is called when the user clicks on an employee list item.

], function (BaseController) {
	"use strict";
	return BaseController.extend("sap.ui.demo.nav.controller.employee.EmployeeList", {
		onListItemPressed : function(oEvent){
			var oItem, oCtx;
			oItem = oEvent.getSource();
			oCtx = oItem.getBindingContext();
				employeeId : oCtx.getProperty("EmployeeID")

Finally, we add the handler onListItemPressed for the press event to the EmployeeList controller. In the handler, we determine the EmployeeID of the list item by querying the binding context and accessing the property EmployeeID from the data model.

Then we navigate to the employee route and pass a configuration object on to the navTo method with the mandatory parameter employeeId filled with the correct EmployeeID. The router always makes sure that mandatory parameters as specified in the route’s pattern are set; otherwise an error is thrown.

EmployeeDetailsOf=Employee Details of
EmployeeIDColon=Employee ID:
FirstName=First Name
LastName=Last Name
PostalCode=Postal Code
PhoneHome=Phone (Home)

Add the new texts to the file.

That’s it. You can go to webapp/index.html#/employees and click on any list item to be redirected to corresponding employee’s details. Check also what happens when you directly navigate to the following files:
  • webapp/index.html#/employees/3

  • webapp/index.html #/employees/33