我正在尝试使用本演练中的参数来实现路由:https://sapui5.hana.ondemand.com/1.58.7/#/topic/2366345a94f64ec1a80f9d9ce50a59ef
但是,不是将数据作为路径获取
/Invoices(CustomerName='Alfreds%20Futterkiste',Discount=0f,OrderID=10835....
当他们这样做时:
oRouter.navTo("detail", {invoicePath:
oItem.getBindingContext("invoice").getPath().substr(1)});
当我在控制器中使用该函数时(请参见下面的onFwdDetail),我只会得到一个字符串路径:
nodes/0
这不能用于路由(请参阅下面的manifest.json):
Invalid value "nodes/0" for segment "{detailPath}".
我认为这是因为我的JSON文件的结构与演练中的结构不同。如何获得带有用于路由的数据的正确路径?
我的实现的相关部分如下:
Data.JSON
{
"nodes": [
{
"text": "Text1",
"status1": "Status10",
"status2". "Status11"
},
{
"text": "Text2",
"status1": "Status20",
"status2". "Status21"
},...]
}
Overview.view.xml
<Table
items="{path: 'mydata>/nodes'}">
...
<ColumnListItem type="Navigation" press="onFwdDetail">
Overview.controller.js
onInit : function() {
var oModel = new JSONModel("model/Data.JSON");
this.getView().setModel(oModel, "mydata");
},
onFwdDetail : function(oEvent) {
var oItem = oEvent.getSource();
var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.navTo("detail", {detailPath:
oItem.getBindingContext("mydata").getPath().substr(1)});
console.log(oItem.getBindingContext("mydata").getPath().substr(1));
}
manifest.json
{
"pattern": "details/{detailPath}",
"name": "details",
"target": "details"
}
答案 0 :(得分:1)
它们的路径是到 OData 模型的实体的路径。您的路径是 JSON 模型的实体的路径。
这些路径的构建完全不同。最重要的是,您的路径中包含斜线,而他们的路径中没有。这会使试图匹配details/{detailPath}
和details/nodes/0
的解析器感到困惑。
在您的示例中,0
本身是有效路径。 nodes
是一个数组,可以执行nodes[0]
。只是路由类不喜欢这种格式。
因此,您可以做的就是将0
传递到detailPath
。然后,您可以在详细视图中构建原始密钥("nodes/" + detailPath
)并将视图绑定到该密钥。
我还将为OData btw推荐这种方法:
OData模型的伪代码:
控制器A:
// read relevant values from binding context
var oContext = oItem.getBindingContext("myModel");
var sKeyName = oContext.getObject("CustomerName");
var sKeyId = oContext.getObject("OrderID");
// trigger navigation
oRouter.navTo("orderDetail", { name: sKeyName, id: sKeyId });
控制器B:
_onRouteMatched: function (oEvent) {
var oModel = this.getModel("myModel");
var that = this;
// read params from routing
var sKeyName = oEvent.getParameter("arguments").name;
var sKeyId = oEvent.getParameter("arguments").id;
// as soon as the metadata of the model are available there is a great API to build keys
oModel.metadataLoaded().then(function () {
var sPath = oModel.createKey("/Invoices", {
CustomerName: sKeyName,
OrderID: sKeyId
});
// sPath should be something like "/Invoices(CustomerName='Troll',OrderID=12345)"
that.getView().bindElement({ path: sPath });
});
},
manifest.json
"pattern": "order/{name},{id}",
"name": "orderDetail"