0

I am trying to bind data in a dropdown (sap.m.Select) from a service but data is not getting displayed in the dropdown. Below is my code:

Controller

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "sap/ui/model/json/JSONModel"
], function (Controller, JSONModel) {
  "use strict";

  return Controller.extend("hmel.TravelandGuestHouse.controller.CloneTravelRequest", {
    onInit: function () {
      this.router = sap.ui.core.UIComponent.getRouterFor(this);
      //For Train Name
      this.addmodel = new JSONModel();
      this.getView().setModel(this.addmodel, "Model");
      this.actionTemp = this.getView().byId("trainName").clone();
      this.detailModel = sap.ui.getCore().getModel("detailModel");
      this.getView().setModel(this.detailModel, "detailModel");
      this.router.attachRoutePatternMatched(this._handleRouteMatched, this);
    },

    _handleRouteMatched: function(evt) {
      if (evt.getParameter("name") !== "CloneTravelRequest") {
        return;
      }
      var that = this;
      that.getView().byId("trainName").bindAggregation("items", {
        path: "/TravelPrpTrainDetails",
        template: that.actionTemp
      });
    }
  });
});

View

<m:Select id="trainName" selectedKey="{detailModel>/TrainName}">
  <core:Item key="{ID}" text="{TrainName}"/>
</m:Select>

Data is coming in the below form and I just want to extract TrainName attribute from it:

<entry>
  <id>http://________________________/TravelPrpTrainDetails(36)</id>
  <category
    term="WCFODataService.TravelPropTrainDetails"
    scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"
  />
  <link
    rel="edit"
    title="TravelPropTrainDetails"
    href="TravelPrpTrainDetails(36)"
  />
  <title />
  <updated>2019-01-03T06:02:36Z</updated>
  <author>
    <name />
  </author>
  <content type="application/xml">
    <m:properties>
      <d:SerialNo m:type="Edm.Int32">2</d:SerialNo>
      <d:ID m:type="Edm.Int32">36</d:ID>
      <d:TrainName>AJMER ASR EXPRESS 19611</d:TrainName>
    </m:properties>
  </content>
</entry>

1 Answer 1

0

​Hi Swappy, the solution is pretty simple. Your item's binding should use the same model name of the parent binding. In this case, you should have:

<core:Item key="{detailModel>Value}" text="{detailModel>Value}"/>

In general, you should always use this rule when your model is not the default one (default is when the model has no name).

<ComboBox
    items="{ModelName>/ListOfValues}">
        <core:Item key="{ModelName>Key}" text="{ModelName>Value}"/>
</ComboBox>

If you're using the default model you should use this kind of template:

<ComboBox
    items="{/ListOfValues}">
        <core:Item key="{Key}" text="{Value}"/>
</ComboBox>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.