loio |
---|
e5310932a71f42daa41f3a6143efca9c |
view on: demo kit nightly build | demo kit latest release
In this tutorial, we explain the concepts of data binding in OpenUI5.
Data binding is used to bind UI elements to data sources. This keeps the data in sync and allows data editing on the UI.
For data binding, you need a model and a binding instance: The model holds the data and provides methods to set the data or retrieve it from a server. It also provides a method for creating bindings to the data. When you call this method, a binding instance is created, which contains the binding information and provides an event, which is fired whenever the bound data changes. An element can listen to this event and update its visualization according to the new data.
The UI uses data binding to bind controls to the model which holds the application data, so that the controls are updated automatically whenever application data changes. Data binding is also used in reverse, when changes in the control, for example data entered by the user, cause updates in the underlying application data. This is called two-way binding.
You don't have to do all tutorial steps in order; you can jump directly to any step you want. Just download the code from the previous step, copy it to your workspace, and ensure that the application runs by calling the
webapp/index.html
file.You can view and download the files for all steps in the Demo Kit at Data Binding. Depending on your development environment you might need to adjust resource paths and configuration entries.
For more information check the Downloading Code for a Tutorial Step section of the tutorials overview page Get Started: Setup, Tutorials, and Demo Apps.
- Step 1: No Data Binding
In this step, we create a basic application and simply place some text on the screen using a standardsap.m.Text
control. The text in this control is a hard-coded part of the control's definition; therefore, this is not an example of data binding! - Step 2: Creating a Model
In this step, we create a model. It serves as a container for the data your application operates on. - Step 3: Create Property Binding
Although there is no visible difference, the text on the screen is now derived from model data. - Step 4: Two-Way Data Binding
In the examples we've looked at so far, we've displayed the value of a model property using a read-only field. We'll now change the user interface to display first and last name fields usingsap.m.Input
fields. We're also adding a check box control to enable or disable both input fields. This setup illustrates a feature known as "two-way data binding". As the view now contains more controls, we're also moving the view definition into an XML file. - Step 5: One-Way Data Binding
Unlike the two-way binding behavior we've seen, one-way data binding is also possible. In this case, data travels in one direction only: from the model, through the binding instance, to the consumer (usually the property of a control), but never in the other direction. Let's modify the previous example to use one-way data binding. This shows how you can switch off the flow of data from the user interface back to the model if needed. - Step 6: Resource Models
Business applications often require language-specific (translatable) text used as labels and descriptions on the user interface. - Step 7: (Optional) Resource Bundles and Multiple Languages
Resource bundles exist to enable an app to run in multiple languages without the need to change any code. To demonstrate this feature, let's create a German version of the app – in fact, all we need to do is create a German version of the resource bundle file. In our code, we activate the German locale for the ResourceModel. - Step 8: Binding Paths: Accessing Properties in Hierarchically Structured Models
In Step 6 , we stated that the fields in a resource model are arranged in a flat structure; in other words, there is no hierarchy of properties. However, this is only true for resource models. The properties within JSON and OData models are usually arranged in a hierarchical structure. So, let's explore how to reference fields in a hierarchically structured model object. - Step 9: Formatting Values
We'd also like to provide our users with a way of contacting Harry Hawk, so we're adding a link that sends an e-mail to Harry. To do this, we convert our data in the model to match thesap.m.URLHelper.normalizeEmail
API. As soon as the user changes the name, the e-mail also changes. We need a custom formatter function for this. - Step 10: Property Formatting Using Data Types
OpenUI5 offers a set of simple data types, includingBoolean
,Currency
,Date
andFloat
. You can apply these data types to controls to ensure that the value displayed on the screen is formatted correctly. If the field is open for input, this also ensures that the user input meets the requirements of that data type. Let's add a new field called Sales Amount of typeCurrency
. - Step 11: Validation Using sap/ui/core/Messaging
Up to this point, we've created a currency field that formats itself correctly. The currency data type can also validate user input to ensure it meets currency requirements. However, OpenUI5 manages data type validation functions and doesn't have a built-in mechanism for reporting error messages back to the UI. We therefore need a way to report error messages from validation functions back to the user. In this step, we're enabling validation for the entire app with a feature known as "Messaging". Once this is set up, any validation error messages based on user input get passed toMessaging
, which then connects them to the appropriate view and control that caused the error. - Step 12: Aggregation Binding Using Templates
Aggregation binding, also known as "list binding", lets a control bind to a list within the model data. This binding allows relative binding to the list entries by its child controls. - Step 13: Element Binding
Now, let's do something with that newly generated list. Typically, you use a list to allow selection of an item and then display the details of that item elsewhere. To accomplish this, we use a form with relatively bound controls and bind it to the selected entity via element binding. - Step 14: Expression Binding
An expression binding lets you display a calculated value on the screen, which is derived from values found in a model object. This feature allows you to insert simple formatting or calculations directly into the data binding string. In this example, we're changing the color of the price depending on whether it's above or below a certain threshold. The threshold value is stored in the JSON model. - Step 15: Aggregation Binding Using a Factory Function
Instead of using a single hard-coded template control, we now opt for a factory function to generate different controls based on the data received at runtime. This approach is much more flexible and allows for the display of complex or heterogeneous data.
Related Information