Search code examples
sapui5

Equivalent for `getBindingContextPath` for non Item controls


I have an issue for my app. I want my edit button to send me in a new page ("ModificationPage") where I can modify my info but it needs the number of my "OT" so that I can retrieve the info I already have on the "DetailPage".

This number is actually at the end of my URL (because we navigate to the detail page based on it) but I don't know how to get it because getBindingContextPath does not work for a button.

Detail Page:

Screenshot of the Detail Page

Modification Page:

Screenshot of the Modification Page

DetailPage.view.xml :

<mvc:View controllerName="com.gima.zmaintenanceot.controller.DetailPage"
  xmlns:mvc="sap.ui.core.mvc"
  xmlns="sap.m">
  <Page id="detailPage"
    title="Détails de l'OT : {maintenanceOT>OrderNumber}"
    showNavButton="true"
    navButtonPress="onNavBack"
    showFooter="true"
    showHeader="true">
    <headerContent>
      <OverflowToolbar>
        <ToolbarSeparator/>
        <Button icon="sap-icon://edit" press=".onNavToModif"/>
      </OverflowToolbar>
    </headerContent> 
    <!-- ... -->
  </Page>
<mvc:View>

DetailPage.controller.js

onNavToModif: function (oEvent) {
  var sPath = oEvent.getSource().getBindingContextPath() + "/OrderNumber";
  var sOrderNumber = this.getModel("maintenanceOT").getProperty(sPath);
  this.navTo("modif", {
    OrderNumber: sOrderNumber
  });
},

manifest.json

"routes": [
  {
    "pattern": "modif/{OrderNumber}",
    "name": "modif",
    "target": "modif"
  }
],
"targets": {
  "modif": {
    "viewPath": "com.gima.zmaintenanceot.view",
    "viewName": "ModificationPage",
    "viewLevel": 3
  }
}

Solution

  • Use getBindingContext to access the bound context, then call getPath for the path. For example:

    onNavToModif: function(oEvent) {
      const oContext = oEvent.getSource().getBindingContext(/*modelName*/);
      const sPath = oContext.getPath("OrderNumber");
      // ...
    },
    

    The API getPath awaits also an optional suffix which will be appended to the return value.


    The visibility of getBindingContextPath is protected. Do not use it in application development.