miskohut
miskohut

Reputation: 1017

sap.m.Select - Bind key as Integer

I am using sap.m.Select as a select option for a user and it looks like this:

<Select
    forceSelection="false"
    selectedKey="{Priority}"
    items="{
        path: '/PRIORITY_DDSet',
        formatter: '.formatter.convert',
        sorter: {
            path: 'Value'
        }
    }"
>
    <core:Item
        key="{
            path:'Id',
            formatter: '.formatter.convert',
            type: 'Integer'
        }"
        text="{Value}"
    />
</Select>

As a model, ODataModel is used. The problem is: when change on the sap.m.Select happens, the string key is bound to the OData model like this:

enter image description here

However, the definition of the OData model states, that Priority should be of type Integer.

As you can see in the definition of the Select, I tried to use formatter for the key as well as the type Integer. I also tried to use sap.ui.model.type.Integer as a type but without success. I checked, and the formatter function is called only when loading the Select. When selection is changed in the Select, the formatter function is not called.

Is there a way to do this in XML? Or do I need to code this e.g. in selection changed event?

Upvotes: 1

Views: 1751

Answers (2)

P&#233;ter Cata&#241;o
P&#233;ter Cata&#241;o

Reputation: 467

Remove formatters and add type: 'sap.ui.model.type.Integer' to selectedKey:

<Select 
    forceSelection="false" 
    selectedKey="{path: 'Priority', type: 'sap.ui.model.type.Integer'}"
    items="{ path: '/PRIORITY_DDSet', sorter: { path: 'Value' } }">
    <core:Item 
        key="{path:'Id'}" 
        text="{Value}"/>
</Select>

I'm not sure if that's the reason but value of the Priority property can be used without any extra coding like type conversion to save with e.g. ODataModel.submitChanges()

Upvotes: 2

Boghyon Hoffmann
Boghyon Hoffmann

Reputation: 18044

The reason why the type of the Priority value has changed to string is because the Id inside the key binding has the type Edm.String. If the Priority has the type Edm.Int32, use the corresponding binding type sap.ui.model.odata.type.Int32 instead of the regular sap.ui.model.type.Integer:

<Select
  forceSelection="false"
  selectedKey="{
    path: 'Priority',
    type: 'sap.ui.model.odata.type.Int32'
  }"
  items="{
    path: '/PRIORITY_DDSet',
    sorter: {
      path: 'Value'
    }
  }">
  <core:Item
    key="{Id}"
    text="{Value}"/>
</Select>

List of other possible OData types

As mentioned in this answer, UI5 offers OData types for conversion of data representation whereas two-way data binding support is kept in contrast to the formatter. That is why your formatter function was not called when selection was changed.

Upvotes: 2

Related Questions