簡體   English   中英

oData 模型的 SAP UI5 訪問值

[英]SAP UI5 access value of oData model

這幾周我開始使用 SAPUI5,並構建了我的第一個顯示一些數據的應用程序。 我什至設法進行了一些過濾,但現在我遇到了一個我不明白的錯誤。

我的 oData 模型具有以下結構:

Notice('0000'):
  Value0: abc
  Value1: def
  Value2: ghi


Notice('0001'):
  Value0: abc
  Value1: def
  Value2: ghi

我有一個保存數據的表:

<Table
    id="noticeList"
    class="sapUiResponsiveMargin"
    width="auto"
    items="{
        path : '/Notice',
        sorter : {
            path : 'Value0'
        }
    }">

...一些標題的東西和列delaration...

      <items>
        <ColumnListItem
             id="noticeColumnListItem"
             items="{
                path : '/Notice'
            }">
            <cells>
                <ObjectStatus text="{Value0}"/>
                <ObjectStatus text="{Value1}"/>
                <ObjectStatus text="{Value2}"/>
           </cells>
        </ColumnListItem>
    </items>
</Table>

現在,我想訪問我的模型的數據(無論是在渲染之前還是之后)。 我沒有觸發這個的事件,我只需要模型數據傳輸后的信息。

所以我搜索了一下,發現了以下內容:

var myValue = this.byId("noticeList").getModel().getProperty("/Notice/Value0");

我嘗試了所有可能的路徑,但總是以 myValue 未定義而告終。 當我轉儲我的模型時,我可以看到它包含一個具有所需值的對象 oData。 此外,我的列表顯示正確,但我無法從模型中讀取單個值。

這是我在 Component.js 中實例化它的方式:

config: {
    fullWidth: true,
    resourceBundle: "i18n/messageBundle.properties",
    serviceConfig: {
        name: "MYJOBS",
        serviceUrl: "/sap/opu/odata/sap/PATH_TO_SERVICE"
    }
},

// Read service URL
var sServiceUrl = mConfig.serviceConfig.serviceUrl;

// Create and set domain model to the component

var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, {
    json: true,
    loadMetadataAsync: true
});

oModel.attachMetadataFailed(function() {
    sap.ui.getCore().getEventBus().publish("Component", "MetadataFailed");
}, this);

//var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl);
this.setModel(oModel);

也許我還應該提到我不使用 index.html 來加載應用程序,而是通過 Fiori Launchpad 和 Component.js 來加載應用程序

我在這個問題上花了幾個小時,但找不到解決方案,我希望有人能幫忙...

親切的問候!

如果您正在調用遠程服務,則未定義場景的最常見原因是在其他代碼繼續執行時返回數據的延遲(因為您正在異步調用您的服務)。

如果您試圖對返回的數據采取行動,那么您可以通過以下方式附加事件偵聽器/匿名函數來執行此操作:

oModel.attachRequestCompleted(function(){
                    // do something
                });

在您的場景中,您的控制器代碼可以更改為如下所示:

 onInit: function() {

   var sServiceUrl = "/sap/opu/odata/sap/yourPathSRV/";
   var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl);
   oModel.attachRequestCompleted(function(){
                    // do something
                });
   this.getView().setModel(oModel);
}

伯納德的答案是解決問題的答案!

問題是我試圖過早地訪問數據。 我已經嘗試通過使用自建的睡眠功能來避免這種情況,但這失敗了(因為睡眠只是延長了請求時間)。

這已經成功了:

this.byId("noticeList").getModel().attachRequestCompleted(function(oEvent){

    var model = oEvent.getSource();

    var myNeededValue = model.getProperty("/NoticeSet('0001')/Value0"));

});

非常感謝!

首先,我希望我做對了一切,最后你得到了我的觀點:-)

對於文件夾等的正確元素結構,您應該獲得 Web IDE 的試用版並使用模板“SAPUI5”。 不要低估結構:-)

你的應用程序的結構應該是:
父文件夾
webapp文件夾
現在處於同一級別:component.js / index.html 和文件夾(例如用於視圖和控制器或 i18n)

1) Component.js / manifest.json(在較新的版本中 > 1.32)
--> 它只包含關於模型實例化、i18n 實例、服務 URL、目標和路由模式的信息。 這里沒有模型加載!

config: {
    fullWidth : true,
    resourceBundle: "i18n/messageBundle.properties",
    serviceConfig: {
        type: "OData",  // type is missing
        uri: "/sap/opu/odata/sap/PATH_TO_SERVICE" // use uri as property
    }

},

2)控制器(在onInit-hook-method中)

 onInit: function() {

   var sServiceUrl = "/sap/opu/odata/sap/yourPathSRV/";
   var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl);
   this.getView().setModel(oModel);

}

3)查看(內容區)

<mvc:View controllerName="TEST.controller.Main"
   xmlns:l="sap.ui.layout"
   xmlns:mvc="sap.ui.core.mvc" 
   xmlns="sap.m">
 <App>
  <pages>
   <Page title="{i18n>title}">
    <content>
    <List id="yourListId" items="{/Notice}">
      <StandardListItem description="{Value0}" title="{Value1}"/>
     </List>
    </content>
   </Page>
  </pages>
 </App>
</mvc:View>

4) 后端

--> 您需要實現 GET_ENTITYSET - 讀取“多個”通知實體的方法:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM