André Shevantes
André Shevantes

Reputation: 449

How to determine binding type for items in a sap.m.Select?

When using a sap.m.Input, I can configure the element like in the example below, in order to set the type of the item that is binded to the Input:

<mvc:View>
     <Input value="{ 
         path:'/company/revenue', 
         type: 'sap.ui.model.type.Float'
     }"/>
</mvc:View>

this way, when I retrieve the property inside '/company/revenue', its type will always be a JavaScript number. However, is it possible to apply a similar "typing" to a sap.m.Select? The property "selectedKey" inside a sap.m.Select always returns a JavaScript String, but I would like to type it to a number like I did with the sap.m.Input above. How can I do it? Thanks in advance!

Upvotes: 0

Views: 107

Answers (1)

schnoedel
schnoedel

Reputation: 3948

Yes, you can do that. It's just a normal property binding.

But beware that the keys of your items inside the Select control have to be compatible with the Float type. The sap.ui.model.type.Float without any formatOptions generates locale dependant strings. So in germany f.e. you get a , as decimal separator and in the US it would be ..

A good idea would be to use a aggregation binding to create the items and configure the keys of the items with the same type as the selectedKey property of your select. See example on JSbin.

  <Select 
     selectedKey="{path:'/selectedKey',type: 'sap.ui.model.type.Float'}"
     items="{/items}">
    <c:ListItem 
      key="{path: 'key', type: 'sap.ui.model.type.Float'}" 
      text="{text}"/>
  </Select>
onInit:function(){
  var data = {
    selectedKey:3,
    items: [
      { key: -1, text: "Item 1" },
      { key: 1.234, text: "Item 2" },
      { key: 3, text: "Item 3" },
      { key: 1234, text: "Item 4" }
    ]
  };
  var model = new sap.ui.model.json.JSONModel(data);
  this.getView().setModel(model);
}

Upvotes: 2

Related Questions