sapui5 - `this.oType.formatValue` 不是函数
问题描述
我正在尝试创建一个自定义类型,如下所示:
return {
create: function (bSign, sUnit, iDecimal) {
return sap.ui.model.SimpleType.extend("type.NumUnit", {
formatValue: function (iValue, sInternalType) {
if (bSign) {
const iNeg = iValue * -1;
return `${iNeg} ${sUnit}`;
}
return `${iValue} ${sUnit}`;
},
parseValue: function (iValue, sInternalType) {
//Split from unit
const [iQuantity] = iValue.split(" ");
return Util.convertStrToFloat(iQuantity, iDecimal);
},
validateValue: function (iValue) {
return iValue;
}
});
}
};
并像这样使用它:
const oNumInput = new sap.m.Input(sId, {
maxLength: oData.NumberDigits,
type: sap.m.InputType.Number,
showValueHelp: oData.WithValues
});
const oType = UnitType.create(oData.Sign, oData.UnitTExt, oData.NumberDecimals);
oNumInput.bindProperty("value", {
path: "value",
type: oType
});
return oNumInput;
编译器抱怨:
PropertyBinding-dbg.js:91 Uncaught (in promise) TypeError: this.oType.formatValue is not a function at constructor.P._toExternalValue (PropertyBinding-dbg.js:91) at constructor.P.getExternalValue (PropertyBinding-dbg.js:77) at f.g.updateProperty (ManagedObject-dbg.js:3367) at constructor.v (ManagedObject-dbg.js:3209) at constructor.a.fireEvent (EventProvider-dbg.js:228) at constructor.B._fireChange (Binding-dbg.js:271) at constructor.O.checkUpdate (ODataPropertyBinding-dbg.js:133) at constructor.O.initialize (ODataPropertyBinding-dbg.js:48) at f.g._bindProperty (ManagedObject-dbg.js:3310) at f.g.updateBindings (ManagedObject-dbg.js:4049)
我究竟做错了什么?
解决方案
类型错误:this.oType.formatValue
当分配的类型不是sap.ui.model.Type
.
根据您的代码,SimpleType.extend
返回一个您仍然必须从中创建实例的模块。因此,您不能只将模块分配给type:
when 绑定属性。您仍然需要使用以下命令调用构造函数new
:
const Type = UnitType.create(oData.Sign, oData.UnitTExt, oData.NumberDecimals);
oNumInput.bindProperty("value", {
path: sPath, /*E.g.: "CharInput>/ZPM_TEST_7" and NOT a static value*/
type: new Type()
});
然后,创建一个应该具有该formatValue
方法的新实例。
推荐阅读
- c++ - C++ isPrime 函数:如何从采用整数参数的函数返回布尔值?
- c++ - 如何使用私有构造函数和方法来实例化对象并设置对象的成员变量?
- sorting - 如何在链表的 add 方法中对节点进行排序?
- c - 使用 GDB 调试注入程序的 DLL
- bash - 在 bash 脚本中传递“~”时找不到“~”。有什么区别?
- javascript - 生成一堆随机像素位置的正确方法是什么?
- python - 在 CSV 中组合类似的列
- java - 我无法使用 Oracle Sql 查询在 java 中设置参数
- xslt - 与 XPath 表达式相比,非流模式下的 XSLT 3 累加器是否提供性能优势?
- python - 使用python的for循环中没有多个名义属性的数据图