Skip to Content

Unable to add Items to select in SAP UI5

Hi Folks,

I have created a sapui5 application in which i need to generate select items dynamically.

I am fetching sub-departments by using department in function import.

 f {bAllowTextSelection: true, mEventRegistry: Object, sId: "__xmlview1--ab", mProperties: d, mAggregations: Object…}
View1.controller.js?eval:37 Object {__metadata: Object, results: Array[5]}
View1.controller.js?eval:49 HR-Others
View1.controller.js?eval:49 HR-Payroll
View1.controller.js?eval:49 HR-Recruitment
View1.controller.js?eval:49 HR
View1.controller.js?eval:49 Academy

As you can see my function import is working successfully, for 'HR' department am getting corresponding sub-departments.

Now what i have done in my view for sub-departments -

<Label text="Select Sub-Department" required="true" />

     <Select width="60%" id="ab"></Select>

And here is my controller -

   var odata1 =
   {
   'Department':selected_dep
   };
    var select_sub = this.getView().byId("ab");
    console.log(select_sub);
  oModel.callFunction('/SubDpByDp',"GET",odata1,null,function(oData,response)
   {
   console.log(oData);
   MessageToast.show("success");
   for(var set of oData.results)
  {
var item = new sap.ui.core.Item();
   item.text= set.SUBDEPARTMENT;
   console.log(item.text);
 select_sub.addItem(item);
   }
    },function(err){
   MessageToast.show("faliure");
     },false
     );

But still select is populating nothing.

Kindly suggest.

Regards,

Ankit

capture.jpg (13.5 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    avatar image
    Former Member
    Mar 09, 2017 at 03:48 PM

    Hi Ankit,
    Try this code .

    View code :

    <Page title="{i18n>title}">
    <content>
    <Label text="Dynamic Select"/>
    <Select items="{/Department}" id="dynamicBind" >
    <items>
    <core:Item text="{dep}"   />                
    </items>
    </Select>
    </content>
    </Page>
    </pages>

    Controller Code :

    onInit:function(){
    var OData = {
    "Department":[{
    "dep":"A"
    },
    
    {
    "dep":"B"
    },
    {
    "dep":"C"
    }
    
    ]
    };
    var oModel = new JSONModel(OData);
    //get Id of the Select Control and SetMOdel 
    //you can also create name model and binding aggregation with this named model.
    //var selectid= this.getView().byId("dynamicBind").setModel(oModel,"mymodelname");
    var selectid= this.getView().byId("dynamicBind").setModel(oModel);
    
    }

    Regards,
    Abul

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 06, 2017 at 04:24 PM

    usually we don't create item and add them to the list

    you do it via aggregation binding. you just supply the data. the item will be generated

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 08, 2017 at 07:07 PM
    Add comment
    10|10000 characters needed characters exceeded

  • Mar 09, 2017 at 05:44 AM

    Well, I recommend you to use binding concepts. SAP Combobox/Dropdown class contains binding features to fetch hierarchy data from ODATA model and display the same as what we need.

    For ex: I have an array i.e

    -var Array = {dept: [{name: "ECE",exp: "4"},{name: "EEE",exp: "5"}]};

    -Append this array on JSON model

    -Bind JSON model to combobox/dropdown

    -use on select event to get Hierarchy data and display the same on other fields.

    Regards,

    Karthik A

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 09, 2017 at 12:50 PM

    Hi Ankit,

    I have just tried to modify your controller and view code.

    Just try with below modified code.

    Controller :

         var aFilter = [new sap.ui.model.Filter("Department", sap.ui.model.FilterOperator.EQ, selected_dep)];
         console.log(select_sub);
    
         var select_sub = this.getView().byId("ab");
         var that = this;
         oModel.read("/SubDpByDp",{ filters:aFilter,
                             	success:function(oData){
    	    		                 var oJSONModel = new sap.ui.model.json.JSONModel({items : oData.results});
    	    		                     select_sub.setModel("data",oJSONModel);
    	    	                        },
    	    	                error:function(){
    	    		                 MessageToast.show("failure");
    	    	                       }
                                  }
                     );
    

    view:

    <Label text="Select Sub-Department" required="true" />
    <ComboBox id="ab" items="{data>/items}" width="60%">
          <core:Item text="{data>SUBDEPARTMENT}" />
     </ComboBox>
    Add comment
    10|10000 characters needed characters exceeded