Business Example
In this exercise, you will extend the ux402_fullscreen application by adding a custom control. The control shows data about the plane in the Flights table, including the following:
Maximum number of passengers
Current number of passengers booked
This is shown in the following figure:
SAP Business Technology Platform and its tools are cloud services. Due to the nature of cloud software, step procedures, and the naming of fields and buttons may differ from the exercise solution.
Task 1: Create the Custom Control
Steps
Log into your SAP Business Application Studio.
Perform this step as shown in a previous exercise.
In the control folder of the ux402_fullscreen project, create a new file named PlaneInfo.js.
Perform this step as shown in a previous exercise.
In the
PlaneInfo.js
file, create a custom control named PlaneInfo by extending thesap.ui.core.Control
.Use the following code:
Code snippetExpandsap.ui.define([ "sap/ui/core/Control" ], function (Control) { "use strict"; return Control.extend("studen##.com.sap.training.ux402.fullscreen.ux402fullscreen.control.PlaneInfo", { }); } );
Add a metadata section. Add the following
properties
to the metadata definition and assign the given type as atype
attribute. Then save your changes.Properties within the Metadata Section of the PlaneInfo.js File
Property Type seatsMax string seatsOcc string Enter the following code inside the
extend
function:Code snippetExpandmetadata:{ properties: { "seatsMax" : { type: "string" }, "seatsOcc" : { type: "string" } } }
Save your changes.
Task 2: Implement the Renderer
It is necessary to implement the renderer for the PlaneInfo
control. The renderer follows the naming standards for renderer.
Steps
In the control folder of the ux402_fullscreen project, create a new file named PlaneInfoRenderer.js.
Perform this step as shown in a previous exercise.
In the
PlaneInfoRenderer.js
file, create a custom control named PlaneInfoRenderer by extending the basesap.ui.core.Renderer
, assign the extended object to a variable namedPlaneInfoRenderer
, and return the created object to the caller.Use the following code:
Code snippetExpandsap.ui.define([ "sap/ui/core/Renderer" ], function (Renderer) { "use strict"; var PlaneInfoRenderer = Renderer.extend("student##.com.sap.training.ux402.fullscreen.ux402fullscreen.control.PlaneInfoRenderer"); return PlaneInfoRenderer; } );
To enable the incremental DOM-like API, set the
apiVersion
of thePlaneInfoRenderer
object to 2.Add the following code before the
return PlaneInfoRenderer
statement :
Add a function
render
to thePlaneInfoRenderer
object created in step 2. The function takes two arguments. Name the first argument oRm. It will contain a reference to the RenderManager at runtime. Name the second argument oControl. This variable will contain a reference to the control that the renderer should render. Add code to therender
function to render the control. The HTML table control should have three rows of data from the PlaneInfo control properties:seatsMax
andseatsOcc
. Do not put in the icons at this point (as seen at the beginning of the exercise). Now, save your changes.Hint
The table being rendered is an HTML table, not an SAPUI5 table. The data from the PlaneInfo control can be accessed from theoControl
object passed in using the automatically generated getter methods.Use the RenderManager passed in to write out an opening div and write the control data for the
oControl
object passed in. Use theopenStart
function of the RenderManager.Write an HTML table control with three rows of data from the PlaneInfo control properties:
seatsMax
andseatsOcc
. Do not put in the icons at this point (as seen at the beginning of the exercise). Use theopenStart
,openEnd
,close
,attr
, andtext
functions of the RenderManager.Close the table and the div. Use the
close
function of the RenderManager.The implementation should look like the following code:
Code snippetExpandPlaneInfoRenderer.render = function(oRm, oControl) { oRm.openStart("div",oControl); oRm.openEnd(); oRm.openStart("table"); oRm.attr("align", "center"); oRm.openEnd(); oRm.openStart("tr"); oRm.openEnd(); oRm.openStart("td"); oRm.openEnd(); oRm.text(" "+oControl.getSeatsMax()); oRm.close("td"); oRm.close("tr"); oRm.openStart("tr"); oRm.openEnd(); oRm.openStart("td"); oRm.openEnd(); oRm.text(" "+oControl.getSeatsOcc()); oRm.close("td"); oRm.close("tr"); oRm.close("table"); oRm.close("div"); }
Note
For less code, you could use method chaining.Save your changes.
Task 3: Add the PlaneInfoRenderer to the PlaneInfo Control
Steps
Open the
PlaneInfo.js
file and add a reference to the newPlaneInfoRenderer
to the implementation.Open the
PlaneInfo.js
file located in the control folder of your project.Update the code as follows:
Code snippetExpandsap.ui.define([ "sap/ui/core/Control", "student##/com/sap/training/ux402/fullscreen/ux402fullscreen/control/PlaneInfoRenderer" ], function (Control, PlaneInfoRenderer) { "use strict";
Add an attribute
renderer
to the implementation and assign thePlaneInfoRenderer
reference to it.Add the following code after the metadata definition:
Save your changes.
Task 4: Add the PlaneInfo Control to the Application
Steps
Open the
i18n.properties
file and add the following key-value pair. Now, save your changes.Key-value Pair within the i18n.properties File
Key Value planeInfo Plane Infos Perform this step as shown in a previous exercise.
Open the
Flights.controller.js
file and add a reference to the newPlaneInfo
control. Now, save your changes.Open the
Flights.controller.js
file located in the controller folder of your project.Update the code as follows:
Code snippetExpandsap.ui.define([ "sap/ui/core/mvc/Controller", "student##/com/sap/training/ux402/fullscreen/ux402fullscreen/control/HoverButton", "sap/m/MessageToast", "student##/com/sap/training/ux402/fullscreen/ux402fullscreen/control/PlaneInfo" ], /** * @param {typeof sap.ui.core.mvc.Controller} Controller */ function (Controller, HoverButton, MessageToast, PlaneInfo) { "use strict";
Save your changes.
Open the
Flights.view.xml
file. Before the lastColumn
element, add a new mobile-enabledColumn
element with the following attributes to thecolumns
aggregation:Attribute-value Pairs for the Column Element within the Flights.view.xml File
Attribute Value id column6 minScreenWidth Tablet demandPopin true hAlign Center Open the
Flights.view.xml
file.Add the following code between
column4
andcolumn5
definition:Code snippetExpand<Column id="column6" minScreenWidth="Tablet" demandPopin="true" hAlign="Center"> </Column>
Add a
sap.m.Text
element with the following attribute and value to the newly createdColumn
element:Attribute-value Pair for the Text Element within the Flights.view.xml File
Attribute Value id text9 text {i18n>planeInfo} Add the following code inside the new Column definition :
Before the
HoverButton
element of thecells
aggregation, add aPlaneInfo
element with the following attributes and values. Bear in mind thatPlaneInfo
is placed in a project specific namespace. Now, save your changes.Attribute-value Pairs for the PlaneInfo Element within the Flights.view.xml File
Attribute Value seatsMax {Seatsmax} seatsOcc {Seatsocc} id planeInfo1 Add the following code in the
cells
aggregation, before theHoverButton
definition:Save your changes.
Save the
Flights.view.xml
file and run the application.Perform this step as shown in a previous exercise.
Verify that the row in the Flights table now shows the
PlaneInfo
control with the three pieces of data. If the control does not display or the application fails to load, open Chrome DevTools and look at the console for errors.Choose a carrier in the Carriers table.
Verify that the row in the Flights table now shows the
PlaneInfo
control with the three pieces of data.If the control does not display or the application fails to load, open Chrome DevTools by pressing F12 (on your keyboard) and look at the console for errors.
Adjust the PlaneInfoRenderer.js file as needed to get the HTML correct. Use Chrome DevTools as needed to look at the HTML code that is inserted by the custom control. This may take several attempts.
Task 5: Add Icons to the PlaneInfo Control
Steps
Open the
PlaneInfoRenderer.js
file.Perform this step as shown in a previous exercise.
To show clearly what the numbers mean, add icons before the numbers of the
PlaneInfo
table. Show the listed icons at the appropriate position. Use theicon
function of the RenderManager. It takes the URL of the icon as a parameter. The URL prefix for the SAP icons issap-icon://
.Icons within the PlaneInfoRenderer.js File
Icon Infront Of person-placeholder seatsMax suitcase seatsOcc Add the following code before
oRm.text(" "+oControl.getSeatsMax());
:Add the following code before
oRm.text(" "+oControl.getSeatsOcc());
:
Save the
PlaneInfoRenderer.js
file and run the application.Perform this step as shown in a previous exercise.
Verify that the icons are appearing on the PlaneInfo control. You may need a few spaces after the icon to move the numbers away from the icon slightly.
Choose a carrier in the Carriers table.
Verify that the icons are appearing on the PlaneInfo control.
You can now close the browser tab in which your application is running.
Since we will further develop your fullscreen project in a following exercise, you can save the current state of the project by downloading it. Select the UX402_fullscreen project and from the context menu, choose Download.