sapui5 - 模型属性被视为对象而不是 formatOptions 中的字符串
问题描述
我有一个包含标签和输入组件的片段。它的值大多是静态设置的,并且按预期工作:
<Label text="Customer" />
<Input
value="John Smith"
editable="true"
change=".liveChangeVehicleGrossPrice"
/>
<Label text="Price" />
<Input
editable="true"
change=".liveChangeVehicleGrossPrice"
value="{
parts: [
'P02_Model>/VehicleGrossPrice',
'valuta>/convert'
],
type: 'sap.ui.model.type.Currency',
formatOptions: {
showMeasure: false
}
}"
/>
现在我在控制器中创建了一个模型,其中包含要在输入组件中使用的值:一个值(客户)要添加到客户名称字段中,两个属性(groupingSeparator
和decimalSeparator
)用于格式化货币:
var json = {
"groupingSeparator": " ",
"decimalSeparator": ",",
"customer": "John Wayne",
};
var model = new JSONModel(json);
this.getView().setModel(model, "P02_Model");
创建此模型后,我修改片段以使用这些值:
<Label text="Customer" />
<Input
editable="true"
change=".liveChangeVehicleGrossPrice"
value="{P02_Model>/customer}"
/>
<Label text="Price" />
<Input
editable="true"
change=".liveChangeVehicleGrossPrice"
value="{
parts: [
'P02_Model>/VehicleGrossPrice',
'valuta>/convert'
],
type: 'sap.ui.model.type.Currency',
formatOptions: {
showMeasure: false,
groupingSeparator: {P02_Model>/groupingSeparator},
decimalSeparator: {P02_Model>/decimalSeparator}
}
}"
/>
问题是当页面加载时,名称John Wayne
在关联输入中正确映射,但包含货币的输入会有
112[对象对象]323[对象对象]2
而不是112 323,2
.
groupingSeparator
不知何故,与和关联的两个值decimalSeparator
是字符串,被视为对象。为什么?
解决方案
为什么?
这是因为绑定信息对象不是 ManagedObject,而是一个不支持绑定功能的简单对象。您必须在 JS 中执行此操作。
<Input id="myInput" />
onInit: function() {
// ...
const model = this.getView().getModel("P02_Model");
this.byId("myInput").bindValue({
parts: [
"P02_Model>/VehicleGrossPrice",
"valuta>/convert"
],
type: this.createCurrencyType()
});
},
onModelDataChanged: function() {
this.byId("myInput").getBinding("value").setType(this.createCurrencyType());
},
createCurrencyType: function() {
return new CurrencyType({ // required "sap/ui/model/type/Currency"
groupingSeparator: model.getProperty("/groupingSeparator"),
decimalSeparator: model.getProperty("/decimalSeparator")
});
},
推荐阅读
- python - import CatBoostClassifier 对于 apache 请求来说花费的时间太长
- php - 如何在 Centos8 本地安装 no_NO.utf8
- python - 无法使用 folium 更改带时间戳的 geojson 标记的颜色
- vue.js - Vue传单加载时打开弹出窗口
- mysql - 我的 mysql 查询(通过合并、临时列和排序)非常慢
- django - 如果有这样的事情,实现按操作和按行身份验证的最佳方法是什么
- google-bigquery - 使用 Python 客户端的 Google BigQuery API
- vue.js - SCSS 如何加载相对于源文件的样式 url 引用(错误:未找到相关模块)
- javascript - 如何选择具有相同类名的 javaScript 中的所有类?
- c - 在 Windows 10 上的 VSCode 中测试 GLUT 时收到错误消息