javascript 使用 XML-Views 时如何在 SAPUI5 中实现数据绑定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23662102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement DataBinding in SAPUI5 when using XML-Views?
提问by monavari-lebrecht
in SAPUI5 assigning a model or data binding to a table or something is very easy, when using JS-Views. But how can I do this, when using XML-Views?
在 SAPUI5 中,使用 JS-Views 时,将模型或数据绑定分配给表或其他东西非常容易。但是,当使用 XML-Views 时,我该怎么做呢?
<?xml version="1.0" encoding="UTF-8" ?>
<core:View
xmlns:core="sap.ui.core"
xmlns="sap.ui.commons"
xmlns:table="sap.ui.table"
xmlns:html="http://www.w3.org/1999/xhtml"
controllerName="view.Main">
<Panel text="Hello World from an XML view">
<Button text="Button" press="doSomething"></Button>
<table:Table width="100%" visibleRowCount="5" selectionMode="Single" editable="false">
<table:title><Label text="Wochentage"></Label></table:title>
<table:Column>
<Label text="ID" />
<table:template><TextField value="{id}"></TextField></table:template>
</table:Column>
</table:Table>
</Panel>
</core:View>
I dont want to give the table a fix id attribute and implement it by calling
我不想给表一个 fix id 属性并通过调用来实现它
sap.ui.getCore().getElementById("idProductsTable").setModel(
demoJSONModel);
in the controller ... :(
在控制器中...... :(
回答by qmacro
You would normally set a model on a control (either the table directly, or one of its parents) in the controller. So I'm going to assume you're wondering about the latter part of your initial statement: "assigning a data binding to a table ... when using XML-Views".
您通常会在控制器中的控件(直接表或其父项之一)上设置模型。因此,我假设您对初始语句的后半部分感到疑惑:“将数据绑定分配给表......使用 XML 视图时”。
All you have to do is express the aggregation as an XML attribute. So if your demoJSONModel looked like this:
您所要做的就是将聚合表示为 XML 属性。因此,如果您的 demoJSONModel 如下所示:
var demoJSONModel = new sap.ui.model.json.JSONModel({
data : [
{ id : 42 },
{ id : 1.618 },
{ id : 3.14 }
]
});
then you could set the binding for the table's 'rows' aggregation like this:
那么你可以像这样设置表的“行”聚合的绑定:
<table:Table
width="100%"
visibleRowCount="5"
selectionMode="Single"
editable="false"
rows="{/data}">
I've put together an example in JSBinthat shows this.