我有一个XML片段,我需要使用格式化程序。但片段无法识别格式化程序
<core:FragmentDefinition xmlns:core="sap.ui.core" xmlns="sap.m" xmlns:f="sap.ui.layout.form" xmlns:l="sap.ui.layout" xmlns:mvc="sap.ui.core.mvc" controllerName="dol.ui.model.formatter">
<l:Grid defaultSpan="L12 M12 S12" width="auto">
<l:content>
<f:SimpleForm title="Section 1" columnsL="2" columnsM="2" editable="false" emptySpanL="0" emptySpanM="0" labelSpanL="4" labelSpanM="4" layout="ResponsiveGridLayout" maxContainerCols="2" minWidth="1024">
<f:content>
<core:Title text="Sub Section 1.1" />
<Label text="G/L Account" />
<Text text="{path: 'GLACC', formatter: '.formatter.removeLeadingZeros'}" />
</f:content>
</f:SimpleForm>
</l:content>
</l:Grid>
</core:FragmentDefinition>
格式化程序是
sap.ui.define([], function() {
"use strict";
return {
removeLeadingZeros: function(sString) {
console.log(Number(sString).toString());
return Number(sString).toString();
}
};
});
片段从具有控制器定义的视图中插入。在视图中使用类似的格式。但同样不适用于片段。
你如何实例化片段?以编程方式(例如,调用sap.ui.xmlfragment(...))?然后将View的控制器作为Fragment的附加参数 - 默认情况下它无法访问View的控制器,因此无法找到formatter函数。
调用片段时在函数中使用this
属性,默认情况下它将采用父视图格式化程序:
var oFragment = sap.ui.xmlfragment("YourFragment.xml",this);
如果你想保持你的片段独立(不依赖于控制器),这种方法对我有用:
如果你在控制器中写如下,它应该识别格式化程序
sap.ui.define([
//formatter path to call formatter.js file. see example below
"path/models/formatter",
], function(formatter){
"use strict";
return {
formatter: formatter,
};
});
在formatter.js文件中定义格式化程序函数,无论您要将其放置在何处
removeLeadingZeros: function(sString) {
console.log(Number(sString).toString());
return Number(sString).toString();
}
见前。对于formatter.js文件
sap.ui.define([], function() {
"use strict";
return {
removeLeadingZeros: function(sString) {
console.log(Number(sString).toString());
return Number(sString).toString();
}
} });