我正在尝试在XML视图中使用expand
选项,但它没有产生任何数据。
数据来自后端,正如我在“网络”选项下的调试中看到的那样,但在XML视图中似乎存在一些绑定问题。
sap.ui.define([
"sap/ui/core/UIComponent",
"sap/ui/Device",
"sem/stock_app/model/models"
], function(UIComponent, Device, models) {
"use strict";
return UIComponent.extend("sem.stock_app.Component", {
metadata: {
manifest: "json"
},
/**
* The component is initialized by UI5 automatically during the startup of the app and calls the init method once.
* @public
* @override
*/
init: function() {
var url = "/sap/opu/odata/sap/ZMATLIST_SRV_02";
var odata = new sap.ui.model.odata.ODataModel(url, {
json: true
});
this.setModel(odata);
UIComponent.prototype.init.apply(this, arguments);
this.getRouter().initialize();
this.setModel(models.createDeviceModel(), "device");
}
});
});
{
"_version": "1.7.0",
"sap.app": {
"id": "sem.stock_app",
"type": "application",
"i18n": "i18n/i18n.properties",
"applicationVersion": {
"version": "1.0.0"
},
"title": "{{appTitle}}",
"description": "{{appDescription}}",
"sourceTemplate": {
"id": "ui5template.basicSAPUI5ApplicationProject",
"version": "1.40.12"
}
},
"sap.ui": {
"technology": "UI5",
"icons": {
"icon": "",
"favIcon": "",
"phone": "",
"phone@2": "",
"tablet": "",
"tablet@2": ""
},
"deviceTypes": {
"desktop": true,
"tablet": true,
"phone": true
},
"supportedThemes": [
"sap_hcb",
"sap_belize"
]
},
"sap.ui5": {
"rootView": {
"viewName": "sem.stock_app.view.mat",
"type": "XML"
},
"dependencies": {
"minUI5Version": "1.30.0",
"libs": {
"sap.ui.core": {},
"sap.m": {}
}
},
"contentDensities": {
"compact": true,
"cozy": true
},
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "sem.stock_app.i18n.i18n"
}
}
},
"resources": {
"css": [{
"uri": "css/style.css"
}]
},
"routing": {
"config": {
"routerClass": "sap.m.routing.Router",
"viewType": "XML",
"viewPath": "sem.stock_app.view",
"controlId": "idAppControl",
"controlAggregation": "pages"
},
"routes": [{
"name": "r1",
"pattern": "",
"target": "t1"
},
{
"name": "r2",
"pattern": "page2/{noti}",
"target": "t2"
}],
"targets": {
"t1": {
"viewName": "mat",
"viewId": "idmat",
"controlAggregation": "pages",
"viewLevel": 1
},
"t2": {
"viewName": "table",
"viewId": "idtable",
"controlAggregation": "pages",
"viewLevel": 2
}
}
}
}
}
<mvc:View
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
xmlns:core="sap.ui.core"
controllerName="sem.stock_app.controller.mat"
displayBlock="true"
>
<App id="idAppControl">
<Page title="Material Input">
<Label text="Material Selection"/>
<Input id="materialInput"
type="Text"
width="50%"
placeholder="Enter Material"
showSuggestion="true"
showValueHelp="false"
valueHelpRequest="handleValueHelp"
submit="onSubmit"
suggestionItems="{/matlistSet}"
>
<suggestionItems>
<core:Item text="{Matid}"/>
</suggestionItems>
</Input>
<Button text="Get Details" enabled="true" press="myPress"/>
</Page>
</App>
</mvc:View>
<mvc:View
xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
controllerName="sem.stock_app.controller.table"
>
<App id="tableApp">
<Page
title="Table"
showNavButton="true"
navButtonPress="onNavBack"
>
<Table
growing="true"
items="{
path: 'odata>/matlistSet',
parameters: {
expand: 'NP_ON_MATID'
}
}"
itemPress="onListItemPressed"
width="100%"
mode="MultiSelect"
>
<columns>
<Column>
<Text text="Material ID"/>
</Column>
<Column>
<Text text="Category"/>
</Column>
<Column>
<Text text="Material Desc"/>
</Column>
<Column>
<Text text="Plant"/>
</Column>
</columns>
<items>
<ColumnListItem type="Active">
<Text text="{odata>NP_ON_MATID/Matid}"/>
<Text text="{odata>NP_ON_MATID/Category}"/>
<Text text="{odata>NP_ON_MATID/Matdesc}"/>
<Text text="{odata>NP_ON_MATID/Plant}"/>
</ColumnListItem>
</items>
</Table>
</Page>
</App>
</mvc:View>
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/core/routing/History"
], function(Controller, History) {
"use strict";
return Controller.extend("sem.stock_app.controller.table", {
onInit: function() {this.getOwnerComponent().getRouter().getRoute("r2").attachPatternMatched(this.mynav, this);
this.getOwnerComponent().getRouter().setView("table");
},
mynav: function(oeve) {
var data = oeve.getParameters().arguments.noti;
var params = "('" + data + "')?$expand=NP_ON_MATID";
var path = "/matlistSet" + params + "";
this.getView().bindElement(path);
},
onNavBack: function(window) {
var oHistory = History.getInstance();
var sPreviousHash = oHistory.getPreviousHash();
if (sPreviousHash !== undefined) {
window.history.go(-1);
} else {
var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.navTo("overview", {}, true);
}
},
});
});
<?xml version="1.0" encoding="utf-8" ?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:sap="http://www.sap.com/Protocols/SAPData">
<edmx:DataServices m:DataServiceVersion="2.0">
<Schema Namespace="ZMATLIST_SRV_02" xml:lang="en" sap:schema-version="1" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityType Name="matlist" sap:content-version="1">
<Key>
<PropertyRef Name="Matid" />
</Key>
<Property Name="Matid" Type="Edm.String" Nullable="false" MaxLength="18" sap:unicode="false" sap:label="Material" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
<Property Name="Category" Type="Edm.String" Nullable="false" MaxLength="20" sap:unicode="false" sap:label="Char20" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
<Property Name="Matdesc" Type="Edm.String" Nullable="false" MaxLength="40" sap:unicode="false" sap:label="Char" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
<Property Name="Plant" Type="Edm.String" Nullable="false" MaxLength="4" sap:unicode="false" sap:label="Plant" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
<Property Name="Status" Type="Edm.String" Nullable="false" MaxLength="20" sap:unicode="false" sap:label="Char20" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
<Property Name="Qty" Type="Edm.Decimal" Nullable="false" Precision="13" Scale="3" sap:unicode="false" sap:label="Quantity" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
<NavigationProperty Name="NP_ON_MATID" Relationship="ZMATLIST_SRV_02.MATASSOCIATION" FromRole="FromRole_MATASSOCIATION" ToRole="ToRole_MATASSOCIATION" />
</EntityType>
<EntityType Name="matdetails" sap:content-version="1">
<Key>
<PropertyRef Name="Matid" />
</Key>
<Property Name="Matid" Type="Edm.String" Nullable="false" MaxLength="18" sap:unicode="false" sap:label="Material" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
<Property Name="Matitno" Type="Edm.Int32" Nullable="false" sap:unicode="false" sap:label="Number" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
<Property Name="Category" Type="Edm.String" Nullable="false" MaxLength="20" sap:unicode="false" sap:label="Char20" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
<Property Name="Matdesc" Type="Edm.String" Nullable="false" MaxLength="40" sap:unicode="false" sap:label="Char" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
<Property Name="Plant" Type="Edm.String" Nullable="false" MaxLength="4" sap:unicode="false" sap:label="Plant" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" />
</EntityType>
<Association Name="MATASSOCIATION" sap:content-version="1">
<End Type="ZMATLIST_SRV_02.matlist" Multiplicity="1" Role="FromRole_MATASSOCIATION" />
<End Type="ZMATLIST_SRV_02.matdetails" Multiplicity="*" Role="ToRole_MATASSOCIATION" />
<ReferentialConstraint>
<Principal Role="FromRole_MATASSOCIATION">
<PropertyRef Name="Matid" />
</Principal>
<Dependent Role="ToRole_MATASSOCIATION">
<PropertyRef Name="Matid" />
</Dependent>
</ReferentialConstraint>
</Association>
<EntityContainer Name="ZMATLIST_SRV_02_Entities" m:IsDefaultEntityContainer="true" sap:supported-formats="atom json xlsx">
<EntitySet Name="matlistSet" EntityType="ZMATLIST_SRV_02.matlist" sap:creatable="false" sap:updatable="false" sap:deletable="false" sap:pageable="false" sap:content-version="1" />
<EntitySet Name="matdetailsSet" EntityType="ZMATLIST_SRV_02.matdetails" sap:creatable="false" sap:updatable="false" sap:deletable="false" sap:pageable="false" sap:content-version="1" />
<AssociationSet Name="MATASSOCIATIONSet" Association="ZMATLIST_SRV_02.MATASSOCIATION" sap:creatable="false" sap:updatable="false" sap:deletable="false" sap:content-version="1">
<End EntitySet="matlistSet" Role="FromRole_MATASSOCIATION" />
<End EntitySet="matdetailsSet" Role="ToRole_MATASSOCIATION" />
</AssociationSet>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
答案 0 :(得分:3)
在这里,我从您的代码中创建了一个正在运行的示例:https://plnkr.co/edit/a2pcO6。
在生产代码中,删除所有模拟服务器引用。
根据您的服务元数据,matdetails
内的外键同时也是唯一的主键。即如果Matid
与matdetails
个实体相关联,则matlist
中的matdetails
不能是唯一的。
setModel
中的模型名称。
this.setModel(odata, "odata")
<Text text="{Matid}"/>
"results"
。与this issue相同,每个集合中都需要"response"
属性,以便UI5可以正确显示项目。不确定原因,因为我没有任何后端专业知识。让所有实体具有唯一可识别性可能会解决此问题。<App>
。应用程序应只包含一个根元素。如果没有,您可能最终得到this kind of problems。由于您希望在表格中显示所选matdetails
实体的matlist
个实体,因此使用绝对路径/matlistSet
绑定项目并不合理。所以,替换..
items="{ path: 'odata>/matlistSet', parameters: { expand: 'NP_ON_MATID' } }"
..只有items="{odata>NP_ON_MATID}"
。这种绑定现在是相对的。一旦在控制器中完成选定的上下文,将解析属性NP_ON_MATID
。
在patternMatched
上,会显示所选的Matid
值。到目前为止一直很好,但在以下几行中有两种反模式:
var params = "('" + data + "')?$expand=NP_ON_MATID"; var path = "/matlistSet" + params + "";
?$expand
,因为它无法扩展实体。将导航属性改为bindElement
的parameters/expand
。偏离主题,但如果您想要导航回来:从window
删除onNavBack: function(window) { ... }
参数。否则,window
中的window.history.go
不是global window
object,而是后退按钮中的press
event。