How to load array into dropdown inside fragments using SAPUI5?

I have been trying to load array inside fragment but i could not able list the array binded value in dropdown.

Initially, i have stored values into empty array and then passed array into json model to list array values into dropdown.

Here is the code for storing array values and passed to json model:

 var value = oEvent.getSource().getParent().getBindingContext().getPath();
 var valueind = value.split("/");
 this.indexj = valueind[2];


var tabley = this.getView().byId("tablez");
var count = tabley.getItems().length;

 for (var i = 0; i < count; i++) {
 this.ops1 = tabley.getItems()[i].getCells()[0].getValue();

 this.objy= {

    "Opeartion": this.ops1,

 };

 console.log("this.objy", this.objy);
 empOper.push(this.objy);

 }//For Loop Closed

 console.log("empOper", empOper);


 this.objy = sap.ui.core.Fragment.byId("Operationsfragment", "operations");

 var seamoModelccd80 = new sap.ui.model.json.JSONModel(); 
 seamoModelccd80.setData({ 

 empOper: empOper

 });
 this.objy.setModel(seamoModelccd80);


console.log("this.objy", this.objy);
console.log("seamoModelccd80", seamoModelccd80);

Here is the code for fragments view:

  <ComboBox id="operations" value="{path='/empOper'}" editable="true" enabled="true" visible="true" width="auto" valueState="None"
 change="ChangOpera">
     <!--<core:Item text="{Name}"/>-->

   </ComboBox>

Here is the srceenshot which i am trying to achieve:

enter image description here

Any help much appreciated pls...

According to @Voyager answer:

  var sValue = "";
 for (var i = 0; i < count; i++) {
    sValue = tabley.getItems()[i].getCells()[0].getValue();
   empOper.push({
   "Opeartion": sValue
   });
 }

 var seamoModelccd80 = new sap.ui.model.json.JSONModel(); // created a JSON model   
 seamoModelccd80.setData({ // Set the data to the model using the JSON object 
 defined already  

  empOper: empOper

  });

 var oView = this.getView();

 Fragment.load({
   name: "fragments.Operationsfragment.Operations",
   id: oView.getId(),
    controller: this
  }).then(
  function(oComboBox) {
   oView.addDependent(oComboBox);
   oComboBox.setModel(seamoModelccd80);
  }
 );
  <ComboBox id="operations" items="{path: '/empOper', templateShareable: 'false'}">
    <items>
       <core:ListItem key="{Name}" text="{Name}"/>
   </items>
 </ComboBox>

my console log

Answers:

Answer

You have to bind the items aggregation of the combobox properly and give the aggregation a template. Since "items" is not the default aggregation of ComboBox, you have to write the aggregation tag (items) specifically. Aggregation Binding

<ComboBox id="operations" items="{path: '/empOper', templateShareable: 'false'}">
    <items>
        <core:ListItem key="{key}" text="{Operation}"/>
    </items>
</ComboBox>

Objects are saved by reference. This means, when you push the object into you array, you are actually just pushing the reference to the same object inside your loop. You have to push new objects every time. Otherwise, you simply have the same objects just in different positions inside you array multiple times.

var sValue = "";
for (var i = 0; i < count; i++) {
    sValue = tabley.getItems()[i].getCells()[0].getValue();
    empOper.push({
        "key": i,
        "Operation": sValue
    });
}

You have to load the fragment properly. Require the module (sap/ui/core/Fragment) at the top of your file. Fragment Instantiation

var oView = this.getView();

Fragment.load({
    name: "NAMESPACE.PATH.TO.Operationsfragment",
    id: oView.getId(),
    controller: this
}).then(
    function(oComboBox) {
        oView.addDependent(oComboBox);
        oComboBox.setModel(seamoModelccd80);
    }
);

I think/hope this addresses all your issues.

Answer

Declare the JSON Model in the manifest without datasource then set data in the controller and bind your ComboBox.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.