'How to dynamically set binding type's "formatOptions" and "constraints" in XML with binding?

I have a list of elements (OData set) and use a binding to show this list. One field is for a quantity value and this value could sometimes need some decimal places.

The requirement is: only show that amount of decimal numbers that is also available in the OData service.

Annotation techniques can't be used.

I 'hacked' something that is misusing a formatter to update the type of a binding. But this is 'a hack' and it is not possible to convert it to XML views. (The reason is a different handling of the scope the formatter will be called).

So I am searching for a working solution for XML views.

The following code would not work but shows the issue:

new sap.m.Input({ // looking for an XML solution with bindings
  value: {
      path: "Quantity",
      type: new sap.ui.model.type.Float({
          // formatOptions
          maxFractionDigits: "{QuantityDecimals}",
          // ...
      }, {
          // constraints
          minimum: 0
      }),
      // ...
  }   
});

The maxFractionDigits : "{QuantityDecimals}" should be "dynamic" and not a constant value.



Solution 1:[1]

Setting formatOptions and constraints dynamically in XML (via bindings or a declared function) is unfortunately not (yet) supported. But IMHO this is a valid enhancement request that app developers would greatly benefit from, since it encourages declarative programming.

I already asked for the same feature some years ago but in a comment at https://github.com/SAP/openui5/issues/2449#issuecomment-474304965 (See my answer to Frank's question "If XMLViews would allow a way to specify the dynamic constraints as well (e.g. enhanced syntax), would that fix the problem?").

Please create a new issue via https://github.com/SAP/openui5/issues/new with a clear description of what kind of problems the feature would resolve and possibly other use cases (You can add a link to my comment). I'll add my ? to your GitHub issue, and hopefully others too.


I'll update this answer as soon as the feature is available.

Solution 2:[2]

Get your dynamic number from your model and store it in a JS variable.

var nQuantityDecimals = this.getModel().getProperty("/QuantityDecimals");

new sap.m.Input({
    value : {
        path : "Quantity",
        type : new sap.ui.model.type.Float({
            maxFractionDigits : nQuantityDecimals,
            source : {
                groupingSeparator: ",",
                decimalSeparator: ".",
                groupingEnabled: false
            }
        }, {
            minimum:0
        })
    }   
}),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Boghyon Hoffmann
Solution 2 alexP