SAP UI5 data type (data type) study notes

Data types in SAPUI5 are used as a mechanism to validate user input (for example, "hello" is not an acceptable value for an order quantity), and to ensure that data is properly formatted and displayed on the UI (for example, displaying 1234567 as 1,234,567 when needed). When used in conjunction with supported 双向绑定data models - which is the best way to use them - data types ensure that the data in the model is only updated when the user provides a valid value.

User input validation can also be done by writing validation logic for each input field in an event handler; the displayed value can be formatted by writing and using a formatter wherever the value is displayed. But obviously, this approach is not scalable - as the application grows, the amount of work required to maintain development done using this approach increases rapidly.

The SAPUI5 framework comes with some common data types that can be used in applications. These include booleans, dates, and floats. You can adjust the behavior of bindings that use data types by specifying constraints and formatting options. Constraints, such as Float's minimum and maximum values, can be used to limit the range of values ​​that are considered valid. Any user input that violates the constraints is considered invalid input and the corresponding path in the model will not be updated. On the other hand, specify formatting options to configure how certain values ​​should be displayed on the UI (such as Float's groupingSeparator and decimalSeparator). The value of the corresponding path in the model is formatted according to the type's formatting options before being displayed. Additionally, the user can enter either formatted (like 12,345) or unformatted (12345) input, both of which are considered valid.

The following figure shows the SAP UI5 standard Integerdata types and their constraintsusage in XML view:

How to create a SAP UI5 custom data type?

To start creating custom data types, we extend the sap.ui.model.SimpleTypeclass and override the SimpleType3 methods defined in the parent class - parseValue, validateValue and formatValue.

parseValue(sExternalValue)

This method receives the user's input as a parameter. The job of this method is to convert the user's input value (external format) into a suitable internal representation of the value (internal format).

validateValue(sInternalValue)

This method receives the parsed value (ie, the internal representation of the value determined by the parseValue method) and must determine whether the value is valid. If the input is determined to be invalid, an exception of type sap.ui.model.ValidateException should be thrown in this method.

formatValue(sInternalValue)

The method receives the parsed value (inner value) as a parameter, and must return a formatted value (that is, the corresponding outer value). This formatted value is displayed on the UI.

Before we start implementing our custom data type, let's take a quick look at when the framework calls each of these 3 methods. We can see from the figure that the order in which the framework triggers these three methods is parseValue() -> validateValue() -> formatValue().

Guess you like

Origin blog.csdn.net/i042416/article/details/123633519
Recommended