padibro
padibro

Reputation: 1374

Input field with different unit of measure

I have a model filed in my JSONMOdel. The name of field id LENGTH_M (lenght in meters)

In my app I use (view and edit) it into two position (two input https://openui5.hana.ondemand.com/docs/api/symbols/sap.m.Input.html)

I want insert in both case in the model the value 2 (in meters) How can I do it if I want bind 1st and 2nd input with the same model property LENGTH_M ?

DateTimeInput https://openui5.hana.ondemand.com/explored.html#/entity/sap.m.DateTimeInput/properties has a good property displayFormat. I can use it to show the property model in different modes. There is something like that in Input component??

I have tried to use formatter but it does not seem to work...

Upvotes: 0

Views: 2235

Answers (1)

Tiago A.
Tiago A.

Reputation: 1482

Yes it is possible. You need to use a type in the binding. Basically a "type" is associated with the 2 ends of the binding, making conversions to and from the model.

You can create your own binding, for instance, you could store the value in milimeters in the model. The meters input would use a custom binding type that displays 1/100 of the value in the model (and stores the value *100 in the model).

More info


EDIT: Here's an example:

sap.ui.model.SimpleType.extend("Company.ui.model.type.LengthMeters", {
    formatValue: function(oValue) {
        return oValue/100;
    },
    parseValue: function(oValue) {
        return oValue*100;
    },
    validateValue: function(oValue) {}
});

It would be something like this. You put this code anywhere, can be a new file and you sap.ui.require() it, or it can be in the view if you only use it there.

Upvotes: 1

Related Questions