我观看了一些关于导航+在视图之间传递数据的教程,但它在我的情况下不起作用.我的目标是实现以下目标:
导航工作完美,详细信息页面显示,但数据绑定似乎不起作用(没有数据显示)我的想法是将JSON字符串传递给详细信息页面.我怎样才能做到这一点?还是有更优雅的方式?
这是迄今为止的代码:
MainView控制器
sap.ui.controller("my.zodb_demo.MainView", {
onInit: function() {
var oModel = new sap.ui.model.json.JSONModel("zodb_demo/model/products.json");
var mainTable = this.getView().byId("productsTable");
this.getView().setModel(oModel);
mainTable.setModel(oModel);
mainTable.bindItems("/ProductCollection", new sap.m.ColumnListItem({
cells: [new sap.m.Text({
text: "{Name}"
}), new sap.m.Text({
text: "{SupplierName}"
}), new sap.m.Text({
text: "{Price}"
})]
}));
},
onDetailsPressed: function(oEvent) {
var oTable = this.getView().byId("productsTable");
var contexts = oTable.getSelectedContexts();
var items = contexts.map(function(c) {
return c.getObject();
});
var app = sap.ui.getCore().byId("mainApp");
var page = app.getPage("DetailsForm");
//Just to check if the selected JSON String is correct
alert(JSON.stringify(items));
//Navigation to the Detail Form
app.to(page, "flip");
}
});
Run Code Online (Sandbox Code Playgroud)
详细表格视图:
<mvc:View xmlns:core="sap.ui.core" xmlns="sap.m" xmlns:f="sap.ui.layout.form" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc" controllerName="my.zodb_demo.DetailsForm">
<Page title="Details" showNavButton="true" navButtonPress="goBack">
<content>
<f:Form id="FormMain" minWidth="1024" maxContainerCols="2" editable="false" class="isReadonly">
<f:title>
<core:Title text="Information" />
</f:title>
<f:layout>
<f:ResponsiveGridLayout labelSpanL="3" labelSpanM="3" emptySpanL="4" emptySpanM="4" columnsL="1" columnsM="1" />
</f:layout>
<f:formContainers>
<f:FormContainer>
<f:formElements>
<f:FormElement label="Supplier Name">
<f:fields>
<Text text="{SupplierName}" id="nameText" />
</f:fields>
</f:FormElement>
<f:FormElement label="Product">
<f:fields>
<Text text="{Name}" />
</f:fields>
</f:FormElement>
</f:formElements>
</f:FormContainer>
</f:formContainers>
</f:Form>
</content>
</Page>
</mvc:View>
Run Code Online (Sandbox Code Playgroud)
详细表格控制器:
sap.ui.controller("my.zodb_demo.DetailsForm", {
goBack: function() {
var app = sap.ui.getCore().byId("mainApp");
app.back();
}
});
Run Code Online (Sandbox Code Playgroud)
det*_*ail 14
在控制器之间传递数据的推荐方法是使用EventBus
sap.ui.getCore().getEventBus();
Run Code Online (Sandbox Code Playgroud)
您可以在控制器之间定义通道和事件.在您的DetailController上,您订阅了这样的事件:
onInit : function() {
var eventBus = sap.ui.getCore().getEventBus();
// 1. ChannelName, 2. EventName, 3. Function to be executed, 4. Listener
eventBus.subscribe("MainDetailChannel", "onNavigateEvent", this.onDataReceived, this);)
},
onDataReceived : function(channel, event, data) {
// do something with the data (bind to model)
console.log(JSON.stringify(data));
}
Run Code Online (Sandbox Code Playgroud)
在您的MainController上发布事件:
...
//Navigation to the Detail Form
app.to(page,"flip");
var eventBus = sap.ui.getCore().getEventBus();
// 1. ChannelName, 2. EventName, 3. the data
eventBus.publish("MainDetailChannel", "onNavigateEvent", { foo : "bar" });
...
Run Code Online (Sandbox Code Playgroud)
请参阅此处的文档:https://openui5.hana.ondemand.com/docs/api/symbols/sap.ui.core.EventBus.html#subscribe
还有一个更详细的例子:http: //scn.sap.com/community/developer-center/front-end/blog/2015/10/25/openui5-sapui5-communication-between-controllers--using-publish-and -subscribe从- eventbus
尽管这个问题很老了,但这个场景今天仍然有效(这是一个典型的主从/ n 对 1场景)。另一方面,当前接受的解决方案不仅已经过时,而且还是xy 问题的结果。
有更优雅的方式吗?
绝对地。看看这个灵活的列布局教程:https://sdk.openui5.org/topic/c4de2df385174e58a689d9847c7553bd
无论使用什么控件(App
、SplitApp
或FlexibleColumnLayout
),概念都是相同的:
getBindingContext(/*modelName*/)
。navTo
(无需传递整个项目上下文)。patternMatched
详细信息控制器的onInit
.arguments
存储传递的密钥的事件参数来创建相应的密钥,通过该密钥可以唯一标识目标条目。如果是 OData,请使用 API createKey
。bindObject
使用唯一条目的路径进行调用,以便将其上下文传播到详细视图。