OData模型不起作用

时间:2018-01-29 06:41:16

标签: odata sapui5

我正在尝试在XML视图中使用expand选项,但它没有产生任何数据。

Screenshot No Data

数据来自后端,正如我在“网络”选项下的调试中看到的那样,但在XML视图中似乎存在一些绑定问题。

Component.js

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");
        }
    });
});

的manifest.json

{
    "_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);
            }
        },

    });
});

metadata.xml中

<?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>

1 个答案:

答案 0 :(得分:3)

TL; DR

在这里,我从您的代码中创建了一个正在运行的示例:https://plnkr.co/edit/a2pcO6

screenshot of https://plnkr.co/edit/a2pcO6

在生产代码中,删除所有模拟服务器引用。

要修复的问题

后端

根据您的服务元数据,matdetails内的外键同时也是唯一的主键。即如果Matidmatdetails个实体相关联,则matlist中的matdetails不能是唯一的。

前端

模型

  • 您错过了setModel中的模型名称。
    • 根据您的观点传递模型名称:this.setModel(odata, "odata")
    • 或从任何地方删除模型名称。例如:<Text text="{Matid}"/>
  • 根据您的screenshot,您实体集的响应正文不包含ODataModel无法处理的属性"results"。与this issue相同,每个集合中都需要"response"属性,以便UI5可以正确显示项目。不确定原因,因为我没有任何后端专业知识。让所有实体具有唯一可识别性可能会解决此问题。
  • 还值得一读:Stop Using sap.ui.model.odata.ODataModel

UI

  • 您将两次实例化根视图(及其控制器)。请避免这样做。读:
  • 从表格视图中删除第二个<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 + "";
    
    1. Avoid creating entity key(s) manually
    2. 避免手动添加?$expand,因为它无法扩展实体。将导航属性改为bindElementparameters/expand
  • 偏离主题,但如果您想要导航回来:从window删除onNavBack: function(window) { ... }参数。否则,window中的window.history.go不是global window object,而是后退按钮中的press event