In this chapter, we'll work with the life cycle events of our page, which will help you to execute your custom code before or while exiting the page. Life cycle events are very important to understand as they help you to perform cleanup and other important tasks before a user enters or exits a page. For example, you can redirect a user to the login page if a user is trying to access a secured page or you want to abort the user session before the user leaves a secured page. We'll execute an example where you will see how to use a life cycle event to load data before a page loads.
We'll show you how to populate the data from relational Business Objects in two separate tables and how to populate child data based on the selection of a parent row. You will learn how to validate a web form on the client side.
We'll show you how to implement various logic related to UI components, such as hiding/showing UI components, calculating a value based on the value of other fields, enabling a logout feature, and so on. You will also learn how to create dependent lists of values from two different Business Objects.
In this chapter, we will cover the following topics:
After completing this chapter, you will be familiar with the life cycle events of a page, how they work, and how to use them in a VB application. You will be able to use the ADP variable to load data from a Business Object and use it to populate a table. You will be able to perform client-side validation on a web form using JavaScript. You will be able to perform various UI logic in a VB application and you will be able to create dependent lists of values.
To complete this chapter, you need the following:
You can find the full source code used in this chapter here: https://github.com/PacktPublishing/Effortless-App-Development-with-Oracle-Visual-Builder/tree/main/Chapter07.
Each and every page in a VB application goes through a life cycle in which a series of actions or tasks is performed. These actions include the initialization of a page, the initialization of types and variables, the rendering of UI components, allowing a user to save the changes before they exit a page, and the cleanup of the resources after leaving the page. It's really important to understand the life cycle of a page so that you can write your code at the correct life cycle stage.
Similarly, an application and its flows have their own life cycles. An application can contain multiple flows, and a flow can contain multiple pages.
Every stage of the page has associated events that you can use to write your code. For example, you want users to log in before they try to access a secured page, and you want to delete the user's session after they have left that page.
Let's learn about the different life cycle events of a page through the following figure:
The following sections will provide explanations of each and every life cycle event of a page.
This event is triggered before a user tries to navigate from one page to another. Visual Builder will start the navigation to the next page, but it will not crush the state of the previous page.
Let's try to understand how the vbBeforeEnter event is helpful for VB applications. Let's suppose a user is trying to access a secured page for which the user doesn't have permission. In that case, you can cancel the navigation and redirect the user to an "unauthorized" page or the login page. You can write your redirect code in the Action Chain associated with the vbBeforeEnter event.
With this event, you can fetch data from the following variables scope:
This event is triggered after all the page-scope variables are added and initialized with their default values, values from URL parameters, or persisted values. This event is generally used to fetch data from Business Objects or Service Connections for reusability purposes.
For example, you can fetch data from a Business Object or Service Connection on page load and populate various UI components with that data.
With this event, you can fetch data from the following variables:
This event is triggered before page exit. Once all the Action Chains associated with the vbBeforeExit event are complete, the page instance is destroyed, and future attempts to access the page will throw an exception.
For example, let's suppose that a user has filled in a form and is trying to navigate to another page before saving their form. So, in this event, you can execute an Action Chain and warn the user that their unsaved changes would be lost.
This event is triggered when exiting a page. With this event, you can associate the Action Chain that you want to execute after navigating to another page. This event is primarily used to clean up resources.
For example, you can destroy the logged-in user session after a user logs out from an application.
In this section, we learned about various page life cycle events that will help you to execute the necessary code depending on your requirements. In the next section, we'll look at how to use the vbEnter event to load data before the page load.
In the previous section, we learned about the different life cycle events of a page; now we will execute a vbEnter event, which will help to load page data before a page is loaded.
In this section, we will execute a use case that will load data from the Employee Business Object before page load and show the data in a table component. The following are the high-level steps to achieve the use case:
We'll create a new web application with the name chapter7 under our VBCSBook application and develop all the use cases in this web application. Refer to Chapter 3, Exploring Visual Builder Ingredients, specifically the Understanding the application's structure section, to see how to create a new web application.
ADP is one of the out-of-the-box data types. It keeps a collection of data as an array that is accessible in the page/app.
Let's execute this use case.
The following are the steps to create a type and an ADP variable:
The previous steps will create a type and a variable.
In this section, we'll look at how to use the vbEnter event, call the Employee Business Object, and assign the Business Object response to the EmployeeADP variable.
The following are the steps to register the vbEnter event:
The previous action will create a new Action Chain on the page scope.
Action Chain/Results/callRestEndpointEmployee/items to Page/EmployeeADP/data
The following screenshot shows the source and target mapping:
That completes the Action Chain logic.
Important Note:
You can associate multiple Action Chains with a single event listener.
This Action Chain will be fired before the main-start page loads.
In this section, we'll look at how to use ADP variables to populate a table component with data.
The following are the steps to populate data in a table using the ADP variable:
The following screenshot shows the table component:
Preview the page and the data will be loaded in the table component through the vbEnter event. You can use the EmployeeADP variable to populate another component too.
In this section, you learned how to use the vbEnter event to load data on page load. Now you can use this event to execute any code that you want before page load. In the next section, we will work with the parent-child relationship.
In this section, we'll look at how to show relational Business Object data in two different tables. In the first table, we'll show the employee data, and in the second table, we'll show the address of the employee selected in the first table.
The following are the high-level steps to execute the use case:
We'll use the main-start page that we created in the previous section and consider the Employee table as the first table.
The following are the steps to populate the table component with the addresses:
Select filterCriterion and click on the Click to add condition link as shown in the following screenshot:
Notice that the address table will not be populated due to the filter criterion.
In this section, we will implement the logic that will show the address of the selected employee in the address table.
The following are the steps to implement the logic:
This completes the logic for populating the address of a selected employee.
Import data in the Address Business Object, preview the application, then select any one of the rows, and the address table should be populated with the associated employee data, if any.
The following screenshot shows the address of the selected employee:
This is how you can show relational data in two different tables.
In this section, we learned how to show data from two different relational Business Objects using two different tables and show the child data based on the selected parent data. In the next section, we'll look at how to validate web forms on the client side using JavaScript.
Client-side validation is one of the most important things in each and every web or mobile application; it helps to validate the data on the client side before a request is sent to the server. In this section, we'll look at how to validate a form on the client side using JavaScript.
In this section, we'll create a web form to onboard a new employee and will apply client-side validation before we save the data in a Business Object.
The following are the high-level steps to execute this use case:
We'll look at the preceding steps in detail in the upcoming sections.
The following are the steps to create a new web form:
The page looks as shown in the following screenshot:
This completes our web page.
As a next step, we'll create a JavaScript code that will validate the web form. If the values are entered in the required fields, then only save the data into the Business Object; otherwise, show the validation messages.
The following are the steps to create JavaScript code:
PageModule.prototype.validateForm = function(form) {
var myform = document.getElementById(form);
if (myform.valid === "valid") {
return true;
} else {
myform.showMessages();
myform.focusOn("@firstInvalidShown");
return false;
}
}
The previous JavaScript function will validate all the required fields of the web form. If any one of the fields is blank, the function will return false; otherwise, it will return true.
In this section, we'll call the previous JavaScript on button click, and if the JavaScript function returns true, then we'll insert the employee details into the employee Business Object; otherwise, we'll show the validation messages.
The following are the steps to implement the logic:
a. Summary as Success
b. Message as Employee created successfully
c. Display Mode as transient
d. Notification Type as confirmation
That's all the logic to validate the web form and insert data into the Business Object programmatically.
Preview the main-onboard-employee page, click on the + Add Employee button without entering any values for any of the UI components, and notice the inline validation messages:
Now, enter values in all the fields and click on the + Add Employee button. The employee details should be saved into the Employee Business Object and you will see a confirmation message on the page as shown in the following screenshot:
Open the Employee Business Object and you should see data there.
In this section, we learned how to validate a web form on the client side using JavaScript and show the inline messages if values are not entered in the required fields. Next, we'll look at how to implement various logic at the UI level.
In this section, we'll see how to implement different logic for UI components, such as logic for hiding/showing UI components, calculating values, enabling a logout feature, and so on.
In this section, we are going to cover the following:
It's a very frequent requirement to hide or show UI components when some other UI component is interacted with. For example, you might show a form if the user clicks on a button and hide the form when the user clicks on the button again.
The following are the steps to take to hide/show UI components using JavaScript:
Make sure all the Input Text components are inside the form layout.
PageModule.prototype.showHide = function() {
var x = document.getElementById(
"advancesearch").style.display;
if (x === "none") {
document.getElementById(
"advancesearch").style.display = "block";
} else {
document.getElementById(
"advancesearch").style.display = "none";
}
}
The following screenshot shows the page:
All the configuration is complete now. Just switch to the live mode of the page. Initially, the form layout will not be visible. Click on the Advance Search button; this will show or hide the form layout.
Sometimes you need to calculate values based on the input of other fields and show the calculated values in a different field. For example, on a web page, there may be a date field that allows you to enter a date of birth, and based on the date of birth entered, a different UI component may display an age figure in years. In this section, we want to execute this use case. So, let's see how to execute such type of use-case requirement using VB.
The following are the steps to calculate age based on date of birth:
PageModule.prototype.calculateAge = function(dateOfBirth)
{
var currentDate = new Date();
var birthDate = new Date(dateOfBirth);
var totalAge = currentDate.getFullYear() – birthDate.getFullYear();
var month = currentDate.getMonth() - birthDate.getMonth();
if (month < 0 || (month === 0 && currentDate. getDate() < birthDate.getDate())) {
totalAge--;
}
return totalAge;
}
Now the configuration is complete. Go to the main-ui-logics page and go to the live mode, enter a date of birth, and you should see that an age is shown, as in the following screenshot:
The value event will be fired as soon as you enter a value in the date of birth field and call the associated Action Chain.
Whenever you develop any application inside the VB, you will see the logged-in user's name in the top-right corner of every page, along with a Sign Out option:
The sign-out button functionality is not enabled by default. You have to enable it manually. The logged-in username and button are set on the shell page of the application.
The following are the steps to enable the logout feature:
Important Note:
out is the ID of the sign-out menu component; hence, we are checking a condition if a user clicks on the sign-out menu, and only then do we allow users to sign out.
The following is the structure of the logout URL:
https://<unique_idcs_identifier>.identity.oraclecloud.com/oauth2/v1/userlogout
You have to replace unique_idcs_identifier with the IDCS identifier of your Oracle Cloud instance.
That's all about enabling a logout feature in a VB application. Run the application and click on the Sign Out button. You will be signed out completely from the VB application.
Whenever you develop a web form, you always need to take valid data from users and restrict users from entering enter junk data into the web form. For example, you will want users to enter only alphabet characters in the name fields and use the correct email address format in the email field.
Oracle VB allows us to validate data using the Validator property of the input components.
The following are the steps to validate an input text field using a regex:
[{"type":"regexp", "options":{"pattern":"[a-zA-Z ]*$", "hint":"Enter a valid value.", "messageDetail": "Only characters are allowed"}}]
Run your page, enter an invalid value in the first name field, and the component should throw an error, as in the following screenshot:
The Validator property of the input component accepts the JSON array in the previous format. You can use the same format to validate other components too and provide different regexes in the pattern property.
In this section, we learned how to implement logic for UI components, such as logic for hiding or showing UI components, calculating values based on another value, enabling a logout feature, and validating input using the Validator property. In the next section, we'll look at how to create dependent Select (Single) components.
Dependent Lists of Values (LOVs) are when two or more LOVs are created, and the values of one LOV depend on the values of the other LOV. For example, the values of a State LOV may depend on the values of a Country LOV.
In this section, we'll create two LOVs using the Select (Single) component. In the first LOV, we'll populate email address using the Employee Business Object, and in the second LOV, we'll populate inventory name from the InventoryDetails Business Object.
The following are the steps to create dependent LOVs:
The configuration is complete now. Switch the page to live mode, select any one of the email addresses from the first LOV, and the corresponding inventory name should be populated in the inventory details LOV if available.
The following screenshot shows the dependent LOVs:
This is how you can create dependent LOVs.
In this section, we learned how to create dependent LOVs using Business Objects. You can use a similar approach to create dependent LOVs from Service Connections.
In this chapter, we learned about the life cycle events of a page and the significance of each life cycle event in detail with examples. This will help you to implement logic at the correct stage based on your web/mobile application's requirements. You learned how to use one of the out-of-the-box events, vbEnter, to load data on page load. We created an ADP variable to load data and used the variable to populate a table.
You learned how to use two related Business Objects to populate data in two tables and populate a child table data based on the row selected in the parent table. With this, you learned how to use filterCriterion to filter data on a page. You learned how to use JavaScript to validate web forms on the client side.
You learned how to implement logic in the UI layer, such as for hiding or showing UI components based on interaction with other components, calculating age based on date of birth using JavaScript, and enabling a logout feature for your VB application, and you learned how to validate UI components using the Validator property. You also learned how to create dependent LOVs using Business Objects.
Along with what you learned in the previous chapter, now you will be able to develop a mid-sized application.
In the next chapter, we'll work with another set of UI components, including checkboxes, dialog boxes, and list components. We'll implement sorting, searching, and pagination for table components and will work with other different visualization components such as PIE, BAR, and AREA.
3.136.97.64