cancel
Showing results for 
Search instead for 
Did you mean: 

combo box binding inside controller

justin_kemp
Participant
0 Kudos

Hi

I am using XML views and want to bind a combo box control to a json model defined in init method.

I dont want to use sap.ui.getCore().setModel() since it breaks another binding in my case. I am looking to bind combo box only with this model.

My code is as below:

<ComboBox id ="cmb1"

  items="{

  path: '/items',

  sorter: { path: 'key' }

  }">

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

  </ComboBox>

var mData = { 

//         "selected":["0","1"], 

            items:[ 

                {key:"0",text:"Display"}, 

                {key:"1",text:"Edit"}

              

            ] 

   }; 

var xmodel = new sap.ui.model.json.JSONModel(mData);

oComboBox = sap.ui.getCore().byId("cmb1");

//oComboBox.bindElement("/xmodel/items");                //does not work.

this.getView().byId("cmb1").setBindingContext("/xmodel/items/0") ;

Accepted Solutions (1)

Accepted Solutions (1)

justin_kemp
Participant
0 Kudos

Resolved the issue by changing

oComboBox = sap.ui.getCore().byId("cmb1");  to


oComboBox = this.getView().byId("cmb1");




But both should work as per my understanding. what is the difference here?

junwu
Active Contributor
0 Kudos

if it is xml view, always use this.getView().byId("cmb1");

sap.ui.getCore() works on real ID.

in xml, if you set id cmb1, the real id is someprefix__cmb1, only this id is recognizable for sap.ui.getCore()

Answers (2)

Answers (2)

junwu
Active Contributor
0 Kudos

oComboBox.setModel(xmodel);


this.getView().byId("cmb1").setBindingContext("/xmodel/items/0") ; //no need

justin_kemp
Participant
0 Kudos

I tried this earlier only, get error:

sap-ui-core.js:152 Uncaught TypeError: Cannot read property 'setModel' of undefined

saivellanki
Active Contributor
0 Kudos

Hi Justin,

If you already set a core model with sap.ui.getCore().setModel(). Did your try saving the combobox items in the property of Global Model? Something like:


var oModel = sap.ui.getCore().getModel();

oModel.setProperty("/oComboBoxItems", mData);

And in the XML view you can give the above binding path for 'items' aggregation. Here is a sample: Plunker  (Where I am using single model with different path for table and combobox)

(or)

an alternative would be using named models.


Regards,

Sai Vellanki.

justin_kemp
Participant
0 Kudos

I want to use two different models. One odata model that is bound globally. Other JSON model just for this combo box