代码之家  ›  专栏  ›  技术社区  ›  Ajay

OData模型不工作

  •  3
  • Ajay  · 技术社区  · 6 年前

    我正在尝试使用 expand 选项,但它不会产生任何数据。

    Screenshot No Data

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

    组成部分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");
            }
        });
    });
    

    显示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);
                }
            },
    
        });
    });
    

    元数据。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 回复  |  直到 6 年前
        1
  •  3
  •   Boghyon Hoffmann    3 年前

    需要解决的问题

    后端

    • 根据您的服务元数据 外键 在…内 matdetails 也是唯一 主键,主键 同时即 Matid s英寸 MAT详细信息 如果有,则不能唯一 matlist 应该与多个 MAT详细信息 实体。

      确保主键(或它们的组合)都是 唯一的 在整个实体集中。

    • 根据您的 screenshot ,实体集的响应正文不包含属性 "results" 这就是 OData V2兼容(与 this issue . 另请参见 difference between OData V1 and OData V2 ). 这个 "response" 属性需要存在于每个集合中,以便UI5可以正确显示项目。

      确保响应主体 OData V2兼容 .

    前端

    模型

    用户界面

    • 根视图及其控制器正在实例化两次。为了避免这种情况,请阅读:

    • 应用程序包含 <App> 多次。典型的UI5应用程序应仅包含一个 根元素 . 如果没有,你最终可能会 this kind of problems .

      删除所有 <应用程序(>); 来自除根视图以外的视图。

    • 因为您想显示 MAT详细信息 所选实体 材料清单 表中的实体,将其中的项与绝对路径绑定 /matlistSet 没有道理。

      代替

      items="{
        path: 'odata>/matlistSet',
        parameters: {
          expand: 'NP_ON_MATID'
        }
      }

      ... 具有 items="{ path: 'odata>NP_ON_MATID' , templateShareable: false }" . 此绑定现在是 相对的 . 导航属性 NP_ON_MATID 将在控制器中给定所选上下文后解析。另请参见 Binding Path 从文档中。

    控制器

    • 在…上 patternMatched ,选定的 马提德 值已给定。到目前为止还不错,但有两个 反模式 在以下行中:

      var params = "('" + data + "')?$expand=NP_ON_MATID";
      var path = "/matlistSet" + params + "";
      1. → Avoid creating entity key(s) manually .
      2. 避免附加 ?$expand 手动,因为它不会展开实体。将导航属性传递给 parameters/expand 属于 bindElement / bindObject 相反
    • 偏离主题,但如果要向后导航:删除 window 参数来自 onNavBack: function(window) { ... } . 否则 在里面 window.history.go 不是全局的 对象,但 press “后退”按钮中的事件。