如何传递值以控制片段声明中的属性?
How can I pass values to control properties from a fragment declaration?
假设我有一个片段,我想在我的 xml 视图中重复使用:
<core:Fragment fragmentName="<em><name></em>" type="XML" />
在该片段中,我有许多控件。我想将一个值传递给该片段以设置这些控件的属性,比如 width
。这是其他框架的基本功能,例如EmberJS:https://guides.emberjs.com/release/components/component-arguments-and-html-attributes/
如何在 SAPUI5 中实现这一点?
我无法从父视图向片段传递参数,对吗?我不会进行对话或可以从控制器初始化的类似内容。实际上,我试图通过用可重复使用的 control/component/fragment/whatever.
替换他们需要的 xml 的 7-8 行来使智能 table 中的列定义更易于维护
所以,经过更多的研究,我想做的事情似乎只能通过“自定义控件”来完成,它需要在 JS 中以编程方式创建:
https://openui5.hana.ondemand.com/topic/39d25641086c42aaa745028e15562ba8
您可以在片段周围添加一些元数据,将它们升级为复合控件。请参阅此处的文档:https://sapui5.hana.ondemand.com/1.86.0/#/topic/b83a4dcb7d0e46969027345b8d32fd44
具有一些默认设置(如列表增长)和一些动态设置(如标签中的文本或片段中列表的绑定)的工作示例
MyControl.js
window.sap.ui.define([
"sap/ui/core/XMLComposite"
], function(XMLComposite) {
"use strict";
return XMLComposite.extend("my.name.space.controls.MyControl", {
metadata: {
library: "my.name.space.controls",
aggregations: {
items: {
type: "sap.m.ListItemBase",
multiple: true,
forwarding: { idSuffix: "--list", aggregation: "items" }
}
},
properties: {
label: { type: "string", defaultValue: "" },
text: { type: "string", defaultValue: "" },
noDataText: { type: "string", defaultValue: "No data" }
}
}
});
});
和MyControl.control.xml
<core:FragmentDefinition xmlns="sap.m"
xmlns:core="sap.ui.core">
<VBox class="sapUiSmallMarginBottom">
<Label id="textLabel"
text="{$this>label}"
class="sapUiTinyMarginEnd"
labelFor="text"
visible="{= ${$this>text} !== '' }" />
<Text id="text"
text="{$this>text}"
visible="{= ${$this>text} !== '' }" />
<Label id="listLabel"
text="{$this>label}"
class="sapUiTinyMarginEnd"
labelFor="list"
visible="{= ${$this>text} === '' }" />
<List id="list"
class="tagList"
enableBusyIndicator="true"
backgroundDesign="Transparent"
growing="false"
showSeparators="None"
noDataText="{$this>noDataText}"
visible="{= ${$this>text} === '' }" />
</VBox>
</core:FragmentDefinition>
然后为了使用确保导入命名空间
<mvc:View
xmlns:reuse="my.name.space.controls"
...
<reuse:MyControl id="asd"
label="{i18n>asd}"
text="{odata>asd}" />
...
假设我有一个片段,我想在我的 xml 视图中重复使用:
<core:Fragment fragmentName="<em><name></em>" type="XML" />
在该片段中,我有许多控件。我想将一个值传递给该片段以设置这些控件的属性,比如 width
。这是其他框架的基本功能,例如EmberJS:https://guides.emberjs.com/release/components/component-arguments-and-html-attributes/
如何在 SAPUI5 中实现这一点?
我无法从父视图向片段传递参数,对吗?我不会进行对话或可以从控制器初始化的类似内容。实际上,我试图通过用可重复使用的 control/component/fragment/whatever.
替换他们需要的 xml 的 7-8 行来使智能 table 中的列定义更易于维护所以,经过更多的研究,我想做的事情似乎只能通过“自定义控件”来完成,它需要在 JS 中以编程方式创建: https://openui5.hana.ondemand.com/topic/39d25641086c42aaa745028e15562ba8
您可以在片段周围添加一些元数据,将它们升级为复合控件。请参阅此处的文档:https://sapui5.hana.ondemand.com/1.86.0/#/topic/b83a4dcb7d0e46969027345b8d32fd44
具有一些默认设置(如列表增长)和一些动态设置(如标签中的文本或片段中列表的绑定)的工作示例
MyControl.js
window.sap.ui.define([
"sap/ui/core/XMLComposite"
], function(XMLComposite) {
"use strict";
return XMLComposite.extend("my.name.space.controls.MyControl", {
metadata: {
library: "my.name.space.controls",
aggregations: {
items: {
type: "sap.m.ListItemBase",
multiple: true,
forwarding: { idSuffix: "--list", aggregation: "items" }
}
},
properties: {
label: { type: "string", defaultValue: "" },
text: { type: "string", defaultValue: "" },
noDataText: { type: "string", defaultValue: "No data" }
}
}
});
});
和MyControl.control.xml
<core:FragmentDefinition xmlns="sap.m"
xmlns:core="sap.ui.core">
<VBox class="sapUiSmallMarginBottom">
<Label id="textLabel"
text="{$this>label}"
class="sapUiTinyMarginEnd"
labelFor="text"
visible="{= ${$this>text} !== '' }" />
<Text id="text"
text="{$this>text}"
visible="{= ${$this>text} !== '' }" />
<Label id="listLabel"
text="{$this>label}"
class="sapUiTinyMarginEnd"
labelFor="list"
visible="{= ${$this>text} === '' }" />
<List id="list"
class="tagList"
enableBusyIndicator="true"
backgroundDesign="Transparent"
growing="false"
showSeparators="None"
noDataText="{$this>noDataText}"
visible="{= ${$this>text} === '' }" />
</VBox>
</core:FragmentDefinition>
然后为了使用确保导入命名空间
<mvc:View
xmlns:reuse="my.name.space.controls"
...
<reuse:MyControl id="asd"
label="{i18n>asd}"
text="{odata>asd}" />
...