Search code examples
bindingodatasapui5

How to correct bind a float property against odata service in SAPUI5?


With neary each version of SAPUI5 I have strange problems with the sap.ui.model.type.Float data type in combination with an ODataModel.

See my old post: How to handle number format of OData Edm.Decimal in sapui5 correct?

Later I build a hack to solve the problem (SAPUI5 1.26.x) that worked and looked like mentioned here: https://stackoverflow.com/a/27147305/3783327

This was well working. Now I try to upgrade to 1.28.12 and to the sap.ui.model.odata.v2.ODataModel model.

I have the following situation:

  • Edm.Decimal on server sending decimals in English locale (seperator is a .)
  • Frontend should handle user language but at least German

I bind the control with the usage of the following type:

return new sap.ui.model.type.Float({
    maxFractionDigits : 2,
    minFractionDigits: 2,
    source : {
        maxFractionDigits : 2,
        minFractionDigits: 2                        
    }
});

According to the documentation the source is used to define the model format.

If I use it as posted the post will have a comma as seperator instead of a dot.

If I don't use it, the model will receive a float instead of a string (same problem as with 1.26.x).

What is the correct solution to bind a Edm.Decimal with the Float datatype so that I can enter the number in the user locale of the browser and it will always be send correct to the server?


Solution

  • Meanwhile I detected following documentation: https://openui5beta.hana.ondemand.com/#docs/guide/91f30dbf6f4d1014b6dd926db0e91070.html

    If link does not work search for sap.ui.model.type.Float in https://openui5beta.hana.ondemand.com

    I use now the following code:

                return new sap.ui.model.type.Float({
                    maxFractionDigits : 2,
                    minFractionDigits: 2,
                    source : {
                        groupingSeparator: ",",
                        decimalSeparator: ".",
                        groupingEnabled: false,
                        maxFractionDigits : 2,
                        minFractionDigits: 2                        
                    }
                });
    

    Looks very complex to just show the Edm.Decimal, not sure if there is a easier solution? I would expect that the client sap.ui.model.odata.v2.ODataModel is smarter, but maybe not?