2015-09-16 45 views
0

我有用於目錄和文件的主/明細應用程序。在我的OData服務中,我有一個導航屬性,從一個目錄到一組文件。我在目錄的文件的詳細視圖中有一個列表。但我無法將其綁定到OData服務的導航屬性如何將列表綁定到導航屬性

<mvc:View xmlns:core="sap.ui.core" xmlns:f="sap.ui.layout.form" xmlns:l="sap.ui.layout" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" controllerName="FileUtility.view.Detail"> 
    <Page id="detailPage" navButtonPress="onNavBack" showNavButton="{device&gt;/isPhone}" title="Files"> 
     <content> 
      <ObjectHeader iconActive="false" id="detailHeader" introActive="false" number="" numberUnit="" title="" titleActive="false"> 
       <attributes id="detailAttributes"> 
        <ObjectAttribute active="false" id="attribute" text="{i18n&gt;detailText}"/> 
       </attributes> 
       <firstStatus id="detailStatus"> 
        <ObjectStatus id="status" text=""/> 
       </firstStatus> 
      </ObjectHeader> 
      <List id="__list0" noDataText="Drop list items here" items="{path :'DirectorySet>/FileSet'}"> 
       <items> 
       <ObjectListItem counter="0" id="__item5" showMarkers="false" title="{Name}" type="Active"> 
       </ObjectListItem> 
       </items> 
       <core:ExtensionPoint name="extDetail"/> 
      </List> 
     </content> 
     <footer id="detailFooter"> 
      <Toolbar id="detailToolbar"> 
       <content> 
        <ToolbarSpacer id="toolbarSpacer"/> 
        <Button icon="sap-icon://action" id="actionButton" press="openActionSheet"/> 
       </content> 
      </Toolbar> 
     </footer> 
    </Page> 
</mvc:View> 

項目路徑是源實體集名稱。不知道我應該得到這個名字。 FileSet部分來自導航屬性。我很困惑如何在視圖中映射它。

編輯:我已經從List標記中刪除了「item = {}」,並試圖在Detail.js文件中將其綁定。

sap.ui.core.mvc.Controller.extend("FileUtility.view.Detail", { 

    onInit : function() { 
     this.oInitialLoadFinishedDeferred = jQuery.Deferred(); 

     if(sap.ui.Device.system.phone) { 
      //Do not wait for the master when in mobile phone resolution 
      this.oInitialLoadFinishedDeferred.resolve(); 
     } else { 
      this.getView().setBusy(true); 
      var oEventBus = this.getEventBus(); 
      oEventBus.subscribe("Component", "MetadataFailed", this.onMetadataFailed, this); 
      oEventBus.subscribe("Master", "InitialLoadFinished", this.onMasterLoaded, this); 
     } 

     this.getRouter().attachRouteMatched(this.onRouteMatched, this); 
    }, 

    onMasterLoaded : function (sChannel, sEvent) { 
     this.getView().setBusy(false); 
     this.oInitialLoadFinishedDeferred.resolve(); 
    }, 

    onMetadataFailed : function(){ 
     this.getView().setBusy(false); 
     this.oInitialLoadFinishedDeferred.resolve(); 
     this.showEmptyView();  
    }, 

    onRouteMatched : function(oEvent) { 
     var oParameters = oEvent.getParameters(); 
     var oView = this.getView(); 
     var sEntityPath = "/" + oParameters.arguments.entity; 

     var oContext = new sap.ui.model.Binding(this.getView().getModel(), sEntityPath + '/FileSet'); 
     this.getView().setBindingContext(oContext); 
     //var oList = oView.byId("__list0"); 
     //oList.bindItems(sEntityPath + '/FileSet',sap.ui.getCore().byId(this.getView().byId('__item5').getId())); 
     //sap.ui.getCore().byId(this.getView().byId('__list0').getId()).bindItems(sEntityPath + '/FileSet',sap.ui.getCore().byId(this.getView().byId('__item5').getId())); 

     //this.bindView(sEntityPath); 

     jQuery.when(this.oInitialLoadFinishedDeferred).then(jQuery.proxy(function() { 


      // When navigating in the Detail page, update the binding context 
      if (oParameters.name !== "detail") { 
       return; 
      } 




      var oIconTabBar = oView.byId("idIconTabBar"); 
      oIconTabBar.getItems().forEach(function(oItem) { 
       if(oItem.getKey() !== "selfInfo"){ 
        oItem.bindElement(oItem.getKey()); 
       } 
      }); 

      //var oList = oView.byId("__list0"); 
      //oList.bindItems(sEntityPath + '/FileSet'); 
      //sap.ui.getCore().byId(this.getView().byId('__list0').getId()).bindItems(sEntityPath + '/FileSet',sap.ui.getCore().byId(this.getView().byId('__item0').getId())); 

      // Specify the tab being focused 
      var sTabKey = oParameters.arguments.tab; 
      this.getEventBus().publish("Detail", "TabChanged", { sTabKey : sTabKey }); 

      if (oIconTabBar.getSelectedKey() !== sTabKey) { 
       oIconTabBar.setSelectedKey(sTabKey); 
      } 
     }, this)); 

    }, 

    bindView : function (sEntityPath) { 
     var oView = this.getView(); 
     oView.bindElement(sEntityPath); 

     //Check if the data is already on the client 
     if(!oView.getModel().getData(sEntityPath)) { 

      // Check that the entity specified was found. 
      oView.getElementBinding().attachEventOnce("dataReceived", jQuery.proxy(function() { 
       var oData = oView.getModel().getData(sEntityPath); 
       if (!oData) { 
        this.showEmptyView(); 
        this.fireDetailNotFound(); 
       } else { 
        this.fireDetailChanged(sEntityPath); 
       } 
      }, this)); 

     } else { 
      this.fireDetailChanged(sEntityPath); 
     } 

    }, 

    showEmptyView : function() { 
     this.getRouter().myNavToWithoutHash({ 
      currentView : this.getView(), 
      targetViewName : "FileUtility.view.NotFound", 
      targetViewType : "XML" 
     }); 
    }, 

    fireDetailChanged : function (sEntityPath) { 
     this.getEventBus().publish("Detail", "Changed", { sEntityPath : sEntityPath }); 
    }, 

    fireDetailNotFound : function() { 
     this.getEventBus().publish("Detail", "NotFound"); 
    }, 

    onNavBack : function() { 
     // This is only relevant when running on phone devices 
     this.getRouter().myNavBack("main"); 
    }, 

    onDetailSelect : function(oEvent) { 
     sap.ui.core.UIComponent.getRouterFor(this).navTo("detail",{ 
      entity : oEvent.getSource().getBindingContext().getPath().slice(1), 
      tab: oEvent.getParameter("selectedKey") 
     }, true); 
    }, 

    openActionSheet: function() { 

     if (!this._oActionSheet) { 
      this._oActionSheet = new sap.m.ActionSheet({ 
       buttons: new sap.ushell.ui.footerbar.AddBookmarkButton() 
      }); 
      this._oActionSheet.setShowCancelButton(true); 
      this._oActionSheet.setPlacement(sap.m.PlacementType.Top); 
     } 

     this._oActionSheet.openBy(this.getView().byId("actionButton")); 
    }, 

    getEventBus : function() { 
     return sap.ui.getCore().getEventBus(); 
    }, 

    getRouter : function() { 
     return sap.ui.core.UIComponent.getRouterFor(this); 
    }, 

    onExit : function(oEvent){ 
     var oEventBus = this.getEventBus(); 
     oEventBus.unsubscribe("Master", "InitialLoadFinished", this.onMasterLoaded, this); 
     oEventBus.unsubscribe("Component", "MetadataFailed", this.onMetadataFailed, this); 
     if (this._oActionSheet) { 
      this._oActionSheet.destroy(); 
      this._oActionSheet = null; 
     } 
    } 
}); 

我已經將綁定代碼添加到「onRouteMatched」方法。

編輯2:

var oList = oView.byId("__list0"); 
var oTemplate = this.getView().byId('__item5'); 
oList.bindItems(sEntityPath + '/FileSet',sap.ui.getCore().byId(oTemplate.getId())); 

我沒有得到任何數據傳回,但儘管被標記爲「無效」

在SAPUI5調試器的路徑是正確的:

我在控制器綁定

編輯3

我得到它在onRouteMatched函數中使用此代碼工作

var oList = oView.byId("__list0"); 
var oTemplate = oView.byId('__item5'); 
oTemplate.bindProperty('title', 'Name'); 
oList.bindItems(sEntityPath + '/FileSet', sap.ui.getCore().byId(oTemplate.getId())); 

回答

1

我想你缺少的只是在詳細視圖上設置綁定上下文(也許你還沒有看到控制器代碼)。
可以說你有2個實體文件夾和文件。您的文件夾具有導航屬性FileSet到文件。
現在您已將文件夾實體綁定到主實體。但是,當您點擊主文件並且詳細信息應該加載文件文件夾,那麼您將會將詳細視圖綁定到主文件中的選定條目。 如何獲得Master視圖中的選定條目?
在對主操作的事件處理程序可以調用

this.getBindingContext().getPath() 

,並通過這種過度到詳細信息視圖。在詳細你可以打電話

var oContext = new sap.ui.model.Binding(oModel,sPath) 
//sPath is the path from master view , oModel is the oData model of your application 
this.getView().setModel(oModel); 
this.getView().setBindingContext(oContext); 
//Now the FileSet of your list will fire and load the data in your list. 

希望這會有所幫助。

+0

感謝您的回覆。我嘗試了你的建議,但沒有奏效。我將編輯帖子並添加Detail.js文件 – TheGreenToaster

+1

您仍然只綁定了主實體,但沒有綁定密鑰。所以可以說實體是文件夾,其中一個的關鍵是Folder1。您需要將詳細視圖綁定爲Folder('Folder1')。因爲FileSet需要一個可以運行導航的值。也要傳遞綁定路徑。可能jsbin或主控制器/視圖代碼將有所幫助。 – Veeraraghavan

+0

如果我設置了一個斷點,sEntityPath的值就是帶有鍵的實體。但是當我看到SAPUI5調試器時,它不會顯示列表的綁定 – TheGreenToaster

1

您正在綁定到已命名的模型,但未在代碼中引用「已命名」模型。

即在你的代碼中有

="{path :'DirectorySet>/FileSet'}"> 
在你的控制器不關你的代碼引用這個模型

例如

this.getView().getModel() 

您應該指定模型的名稱(因爲您可以有幾個模型,並且您似乎綁定到一個命名模型。 如果您最初命名問題DirectorySet模型然後指定在相關的操作 - 例如爲:

this.getView().getModel("DirectorySet") 

我還沒有經歷過所有的代碼,但是這將是一個良好的開端。

編輯: 使用以下方法來獲取實際值:

jQuery.sap.log.setLevel(jQuery.sap.log.LogLevel['INFO']); 
jQuery.sap.log.info("fully qualified path: " + sEntityPath + '/FileSet',sap.ui.getCore().byId(oTemplate.getId())); 

只是想確保我們得到的完整和正確綁定路徑。

+0

感謝您的答覆。我已經從視圖中刪除了路徑,現在正試圖在控制器中實現綁定。我將編輯帖子以顯示當前的綁定。 – TheGreenToaster

+0

sEntityPath的價值是什麼?你可以(如果你還沒有)使用上面的內容來查看瀏覽器控制檯中的實際值,在編輯上面的答案時使用我的建議。 – Bernard

+0

我能夠使用.bindItems將列表綁定到sEntityPath。我將發佈一個修改 – TheGreenToaster

相關問題