sap.m.Select - 将键绑定为整数

时间:2018-01-08 06:21:48

标签: odata sapui5

我使用sap.m.Select作为用户的选择选项,它看起来像这样:

<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>

作为模型,使用ODataModel。问题是:当sap.m.Select发生更改时,字符串键绑定到OData模型,如下所示:

enter image description here

但是,OData模型的定义指出Priority应该是Integer类型。

正如您在Select的定义中所看到的,我尝试将formatter用于key以及类型Integer。我也尝试使用sap.ui.model.type.Integer作为类型,但没有成功。我检查过,只有在加载Select时才调用formatter函数。在Select中更改选择时,不会调用格式化程序功能。

有没有办法在XML中执行此操作?或者我是否需要对此进行编码,例如在选择改变了事件?

2 个答案:

答案 0 :(得分:1)

删除格式化程序并将type: 'sap.ui.model.type.Integer'添加到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>

我不确定这是否是原因,但Priority属性的值可以在没有任何额外编码的情况下使用,例如类型转换以保存,例如ODataModel.submitChanges()

答案 1 :(得分:1)

Priority值的类型已更改为字符串的原因是Id绑定中的key具有类型Edm.String。如果Priority的类型为Edm.Int32,请使用相应的绑定类型sap.ui.model.odata.type.Int32而不是常规的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

正如this answer中所述,UI5为数据表示转换提供OData types,而与formatter形成对比的是双向数据绑定支持。这就是更改选择时未调用格式化程序功能的原因。

相关问题