简体   繁体   中英

How can I set Selected items in a sapui5 MultiComboBox?

I have the following json data:

{  
   "medios":[  
      {  
         "Medio":"Cheque",
         "Key":"5"
      },
      {  
         "Medio":"Transferencia Bancaria",
         "Key":"6"
      }
   ]
}

And I bind this data using a json model:

var oModelTest = new sap.ui.model.json.JSONModel();
var MediosPagoPromesa = [];
var MedioObj = {  
Medio: proMedioPagoCP, //a variable I fill inside a loop
Key: i.toString() //because it is inside a loop
}

MediosPagoPromesa.push(MedioObj);

 oModelTest.setData({
 'medios': MediosPagoPromesa
 });
sap.ui.getCore().setModel(oModelTest, "Pagos"); 

into a MultiComboBox:

 var test = sap.ui.getCore().getModel("Pagos"); 

 var oMultiSelect = new sap.m.MultiComboBox({
      items: {
      path: "/medios",
      template: new sap.ui.core.ListItem({
      key: '{Key}',
      text: '{Medio}'
  }),
      templateShareable: true
      },
      selectedKeys: ?,      //here is my problem             
});
oMultiSelect.setModel(test);

What I don't know, is how can I set as selected Items, all the items I am binding in the MultiComboBox, So they can be automatically displayed as selected even from the first time, any idea idea how can I achieve this?

add a new array of the selected element filed in the the loop

var oModelTest = new sap.ui.model.json.JSONModel();
var MediosPagoPromesa = [];
var selected = [];
var MedioObj = {  
Medio: proMedioPagoCP, //I variable I fill inside a loop
Key: i.toString() //because it is inside a loop
}
selected.push(i.toString); //inside the loop

MediosPagoPromesa.push(MedioObj);

 oModelTest.setData({
 'medios': MediosPagoPromesa,
 'selected' : selected 
 });
sap.ui.getCore().setModel(oModelTest, "Pagos");

In MultiComBox use bindProperty to bind the selectedKeys property

var test = sap.ui.getCore().getModel("Pagos"); 

 var oMultiSelect = new sap.m.MultiComboBox({
      items: {
      path: "/medios",
      template: new sap.ui.core.ListItem({
      key: '{Key}',
      text: '{Medio}'
  }),
      templateShareable: true
      },

});
oMultiSelect.bindProperty("selectedKeys", "/selected");
oMultiSelect.setModel(test);

Here is jsbin with clear example : https://jsbin.com/murural/1/edit?html,js,output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM